Here's a script that will make a random model from the "pet" folder in ReplicatedStorage spawn on a part called "Spawn" in Workspace when a player clicks a TextButton named "DropButton":
-- Server Script (put this in ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local function onDropButtonClicked(player)
-- Get the Spawn part
local spawnPart = Workspace:FindFirstChild("Spawn")
if not spawnPart then
warn("Spawn part not found in Workspace!")
return
end
-- Get the pet folder
local petFolder = ReplicatedStorage:FindFirstChild("pet")
if not petFolder then
warn("pet folder not found in ReplicatedStorage!")
return
end
-- Get all models in the pet folder
local petModels = petFolder:GetChildren()
if #petModels == 0 then
warn("No models found in the pet folder!")
return
end
-- Select a random model
local randomIndex = math.random(1, #petModels)
local randomModel = petModels[randomIndex]:Clone()
-- Position the model at the spawn part
randomModel:SetPrimaryPartCFrame(spawnPart.CFrame)
-- Parent the model to workspace
randomModel.Parent = Workspace
-- Optional: Clean up after some time
-- game:GetService("Debris"):AddItem(randomModel, 60) -- Removes after 60 seconds
end
-- Connect the button click event
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local button = character:FindFirstChild("DropButton")
if button then
button.Activated:Connect(function()
onDropButtonClicked(player)
end)
end
end)
end)
Important Notes:
- This script should be placed in ServerScriptService because it needs to access ReplicatedStorage and Workspace.
- The "DropButton" should be a TextButton that's a child of the player's character (or you can modify the script to find it elsewhere).
- The "pet" folder should contain Model instances (not just parts or other objects).
- The "Spawn" part must exist in Workspace when the script runs.
Alternative Client-Side Version (if you prefer):
If you want the button click to be handled on the client side (though this is less secure), you could use this:
-- LocalScript in StarterPlayerScripts or StarterCharacterScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local button = script.Parent:FindFirstChild("DropButton")
if button then
button.Activated:Connect(function()
local spawnPart = Workspace:FindFirstChild("Spawn")
if not spawnPart then return end
local petFolder = ReplicatedStorage:FindFirstChild("pet")
if not petFolder then return end
local petModels = petFolder:GetChildren()
if #petModels == 0 then return end
local randomIndex = math.random(1, #petModels)
local randomModel = petModels[randomIndex]:Clone()
randomModel:SetPrimaryPartCFrame(spawnPart.CFrame)
randomModel.Parent = Workspace
end)
end
Choose the version that best fits your game's architecture. The server-side version is generally more secure and recommended for most cases.