Got it! The folder is called Pets (with a capital P). Here's the updated server-side script that spawns a random model from the Pets folder in ReplicatedStorage onto the Spawn part in Workspace when a player clicks a TextButton named DropButton.
Server Script (place in ServerScriptService):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
-- Function to spawn a random pet model at the Spawn part
local function spawnRandomPet()
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()
-- Make sure the model has a PrimaryPart set
if not randomPet.PrimaryPart then
-- Try to set PrimaryPart to the first child that is a BasePart
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
-- Listen for players joining and connect the button click
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Wait for the DropButton to exist in the player's GUI
local playerGui = player:WaitForChild("PlayerGui")
local dropButton = playerGui:WaitForChild("DropButton", 10) -- adjust path if needed
if dropButton and dropButton:IsA("TextButton") then
dropButton.Activated:Connect(function()
spawnRandomPet()
end)
else
warn("DropButton not found or not a TextButton in PlayerGui for player "..player.Name)
end
end)
end)
Important:
This assumes DropButton is a TextButton inside the player's PlayerGui.
If your DropButton is somewhere else (like inside the character or a ScreenGui), adjust the path accordingly.
The pet models inside Pets must have their PrimaryPart set, or at least contain a BasePart to assign as PrimaryPart.
The Spawn part must exist in Workspace.
If you want me to help with a LocalScript version or if your button is inside a specific GUI (like a ScreenGui), just let me know!