Hello. I made this spawn button script that spawns random trains.
However it does not work.
Script:
local cooldown = 5
local button = script.Parent
local click = button.activate
local assetpack = game.ServerStorage.trainassets
local trainassets = {
assetpack.train1,
assetpack.train2,
assetpack.train3,
assetpack.train4,
assetpack.train5,
assetpack.train6
}
local spawnpoints = game.Workspace.trainspawnpoints
button.Color = Color3.new(0, 0.615686, 1)
local msg = "Calling in trains..."
local part = script.Parent.Parent.chatpart
local ChatService = game:GetService("Chat")
click.MouseClick:Connect(function()
local firsttrain = assetpack[math.random(#assetpack)]
firsttrain.Position = spawnpoints.SpawnPoint1.Position
firsttrain.Orientation = spawnpoints.SpawnPoint1.Orientation
firsttrain.Parent = game.Workspace
for _, parts in pairs (firsttrain:GetDescendants()) do
if parts:IsA("BasePart") then
parts.Anchored = false
end
end
local secondtrain = assetpack[math.random(#assetpack)]
secondtrain.Position = spawnpoints.SpawnPoint2.Position
secondtrain.Orientation = spawnpoints.SpawnPoint2.Orientation
secondtrain.Parent = game.Workspace
for _, parts in pairs (secondtrain:GetDescendants()) do
if parts:IsA("BasePart") then
parts.Anchored = false
end
end
local thirdtrain = assetpack[math.random(#assetpack)]
thirdtrain.Position = spawnpoints.SpawnPoint3.Position
thirdtrain.Orientation = spawnpoints.SpawnPoint3.Orientation
thirdtrain.Parent = game.Workspace
for _, parts in pairs (thirdtrain:GetDescendants()) do
if parts:IsA("BasePart") then
parts.Anchored = false
end
end
button.activate.MaxActivationDistance = 0
wait(cooldown)
button.activate.MaxActivationDistance = 32
end)
I also want:
the script to delete the previous trains spawned when attempting to spawn again
This will also change the train orientation to spawn part orientation too and make sure you set its primary part
I just noticed that you want to delete the previous trains too. You can use Clone() for that
if workspace:FindFirstChild("FirstTrain") then workspace:FindFirstChild("FirstTrain"):Destroy() end --If the script found the train name "FirstTrain", it will delete it
local firsttrain = assetpack[math.random(#assetpack)]:Clone()
firsttrain.PrimaryPart.CFrame = spawnpoints.SpawnPoint1.CFrame
firsttrain.Name = "FirstTrain" --Name it will help the script to detect the train you want to remove (You can not use it but just for easier)
firsttrain.Parent = game.Workspace
for _, parts in pairs (firsttrain:GetDescendants()) do
if parts:IsA("BasePart") then
parts.Anchored = false
end
end
This is just an example for one train you can just copy pasted for the rests
Directly manipulating the CFrame of a model’s primary part only works in the case that a model has constraints applied and it is unanchored. Otherwise, it would only move the primary part instead of the entire model.
Change the Clone line to assetpack[math.random(#assetpack:GetChildren())]:Clone()
Oh and about setting CFrame with Primary Part it is only work when your trains used constraints to make model. If not i recommend you to use PivotTo()