Model spawning button won't work

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

Properties:
Button:
image
Spawnpoints:
image
Train assets:
image

2 Likes

They probably are going into Workspace but they might be unanchored and are falling out of the world. Are they anchored by any chance?

1 Like

The trains are anchored, by the way.

1 Like

I dont think you can set the position of a model like that. You should set its CFrame instead like:

firsttrain.PrimaryPart.CFrame = spawnpoints.SpawnPoint1.CFrame

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

1 Like

PivotTo is better.

Have only one part anchored. It’s not necessary to anchor everything, unless you’re moving all the parts before you spawn it.

task.wait in this case

and you repeat the same thing 3 times. Try making the multiple models into one if you have to spawn them together.

As a note, this would not work either, and assuming the train is a model it should instead be

firsttrain:SetPrimaryPartCFrame(spawnpoints.SpawnPoint1.CFrame)

additionally, the model’s primary part has to be set in the properties tab for this to work.

Uh no? Using primary part still work i used it everytime and yes you have to set it

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.

Right i forgot about constraints. We dont know if their train have them.

I got this error while trying that.

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()

Try:

local firsttrain = assetpack:GetChildren()[math.random(1,#assetpack:GetChildren())]:Clone()

It fixed the error, but another problem:


It’s only setting the SpawnBox position. (primarypart)
EDIT: :pivotto() fixed it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.