Wait Till Speed Value Is Zero To Start Spawning NPCs

Hello!
I’m trying to make a script on where the train speed is zero, the passengers start to spawn from the coaches.

My script:

local Spawnlocation = script.Parent
local Passenger = game.ReplicatedStorage.Passenger
local Speed = script.Parent.Parent.Parent.Parent.SpeedControl.SpeedScript.Speed.Value
local counter = 0
local Amount = math.random(1,10)

if counter < Amount then
	local NewPassanger = Passenger:Clone()
	NewPassanger.Parent = workspace
	NewPassanger:SetPrimaryPartCFrame(Spawnlocation.CFrame)
	counter = counter + 1
	print(counter)
	end
end

The script works fine, all i need is something to detect the train speed value hitting 0 and then the passengers will start to spawn.

Thanks,
Ruben

(P.S. Sorry if the script may looks a bit messy, I’m quite new to scripting with lua and I’m slowly trying to get the hang of it. :upside_down_face: )

1 Like
local Spawnlocation = script.Parent
local Passenger = game.ReplicatedStorage.Passenger
local Speed = script.Parent.Parent.Parent.Parent.SpeedControl.SpeedScript.Speed
local counter = 0
local Amount = math.random(1,10)

local function SpawnPassengers()
	if Speed.Value <= 0 and counter < Amount then		
		for Count = 1, Amount - counter do
			local NewPassenger = Passenger:Clone()
			
			NewPassenger.Parent = workspace
			NewPassenger:SetPrimaryPartCFrame(Spawnlocation.CFrame)
			counter = counter + 1
			print(counter)
		end
	end
end

Speed:GetPropertyChangedSignal("Value"):Connect(SpawnPassengers)
1 Like
local Spawnlocation = script.Parent
local Passenger = game.ReplicatedStorage.Passenger
local SpeedScript = script.Parent.Parent.Parent.Parent.SpeedControl.SpeedScript
local counter = 0
local Amount = math.random(1, 10)

local function spawnPassengers()
    while counter < Amount do
        local NewPassenger = Passenger:Clone()
        NewPassenger.Parent = workspace
        NewPassenger:SetPrimaryPartCFrame(Spawnlocation.CFrame)
        counter = counter + 1
        print(counter)
    end
end

SpeedScript.Speed.Changed:Connect(function(newSpeed)
    if newSpeed == 0 then
        spawnPassengers()
    end
end)
1 Like

Exactly what I was going for, Cheers!

lol, he just copied me in a worse way… but okay that’s fine.

I tried your way and nothing spawned.

I started writing before you and what does “worse way” mean? Instead of while u can use if maybe this will be better idk

Yeah, that’s why you accidentally quote my script while copying it and removed it quickly xD

Because i forgot a “.” at the top of the function.

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