Perfect! Here’s how you can set it up:
- DropButton is in DropFrame inside Main (a ScreenGui) in the player’s PlayerGui.
- The Pets folder is in ReplicatedStorage.
- The Spawn part is in Workspace.
Recommended Approach
Use a RemoteEvent for security:
- The button click is detected on the client (LocalScript).
- The server handles spawning the pet (Script in ServerScriptService).
1. Create a RemoteEvent
In ReplicatedStorage, create a RemoteEvent called DropPetEvent.
2. LocalScript (handles button click)
Place this LocalScript inside Main.DropFrame.DropButton:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DropPetEvent = ReplicatedStorage:WaitForChild("DropPetEvent")
script.Parent.MouseButton1Click:Connect(function()
DropPetEvent:FireServer()
end)
3. Server Script (handles pet spawning)
Place this Script in ServerScriptService:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local DropPetEvent = ReplicatedStorage:WaitForChild("DropPetEvent")
DropPetEvent.OnServerEvent:Connect(function(player)
local spawnPart = Workspace:FindFirstChild("Spawn")
if not spawnPart then
warn("Spawn part not found in Workspace!")
return
end
local petsFolder = ReplicatedStorage:FindFirstChild("Pets")
if not petsFolder then
warn("Pets folder not found in ReplicatedStorage!")
return
end
local petModels = petsFolder:GetChildren()
if #petModels == 0 then
warn("No models found in Pets folder!")
return
end
local randomIndex = math.random(1, #petModels)
local randomPet = petModels[randomIndex]:Clone()
-- Ensure the model has a PrimaryPart
if not randomPet.PrimaryPart then
for _, part in ipairs(randomPet:GetChildren()) do
if part:IsA("BasePart") then
randomPet.PrimaryPart = part
break
end
end
if not randomPet.PrimaryPart then
warn("Random pet model has no PrimaryPart and no BasePart to assign!")
return
end
end
randomPet:SetPrimaryPartCFrame(spawnPart.CFrame)
randomPet.Parent = Workspace
end)
Summary of Setup
- ReplicatedStorage
Pets (Folder with pet models)
DropPetEvent (RemoteEvent)
- Workspace
- StarterGui
Main (ScreenGui)
DropFrame (Frame)
DropButton (TextButton, with the LocalScript inside)
Let me know if you need help with any part of this setup!