I spawned a train with a good spawner GUI, but it was located at the centre position. Even, when I show output, it says “Argument 1 missing or nil” at line 16. So, however, I scripted and messed up for some train spawner GUI.
local plrEngine = {}
local spawnLocation = workspace.spawnLocation
local replicatedStorage = game.ReplicatedStorage
local spawnTrainEvent = replicatedStorage.SpawnTrainEvent
spawnTrainEvent.OnServerEvent:Connect(function(plr, engineAsset, slocation)
-- Check if the player has already spawned a train
if plrEngine[plr] then
plrEngine[plr]:Destroy()
end
local engine = replicatedStorage.TrainAssets.Locomotives.Steam:FindFirstChild("Thomas"):Clone()
engine.Parent = workspace
local spawnLoc = slocation or spawnLocation:WaitForChild(slocation)
engine:SetPrimaryPartCFrame(CFrame.new(spawnLoc))
plrEngine[plr] = engine
end)
How do I make some trains spawn properly at the same position as the part I named “location” of them?
To ensure that the trains spawn at the same position as the part you named “location”, you can modify your code as follows:
local plrEngine = {}
local replicatedStorage = game.ReplicatedStorage
local spawnTrainEvent = replicatedStorage.SpawnTrainEvent
spawnTrainEvent.OnServerEvent:Connect(function(plr, engineAsset, slocation)
-- Check if the player has already spawned a train
if plrEngine[plr] then
plrEngine[plr]:Destroy()
end
local engine = replicatedStorage.TrainAssets.Locomotives.Steam:FindFirstChild("Thomas"):Clone()
engine.Parent = workspace
local spawnLoc = slocation or workspace.spawnLocation -- Use workspace.spawnLocation directly if slocation is nil
if spawnLoc then
engine:SetPrimaryPartCFrame(CFrame.new(spawnLoc.Position))
plrEngine[plr] = engine
else
warn("Spawn location not found!")
end
end)
I’ve modified the spawn train script. Now for position from the selected location for value? Because ObjectValue doesn’t have CFrame values.
local plrEngine = {}
local replicatedStorage = game.ReplicatedStorage
local spawnTrainEvent = replicatedStorage.SpawnTrainEvent
spawnTrainEvent.OnServerEvent:Connect(function(plr, engineAsset, slocation)
-- Check if the player has already spawned a train
if plrEngine[plr] then
--plrEngine[plr]:Destroy()
end
local engine = engineAsset
if engine then
engine:Clone().Parent = workspace
else
error("Unable to find engine asset!")
end
local spawnLoc = location or workspace.spawnLocation -- Use workspace.spawnLocation directly if slocation is nil
if spawnLoc then
engine:SetPrimaryPartCFrame(CFrame.new(spawnLoc.Position))
plrEngine[plr] = engine
elseif not spawnLoc then
error("Unable to find spawn location!")
elseif not spawnLoc:IsA("Basepart") then
error("Failed to load spawn location for reading.")
end
end)