Hello, I have 2 loops inside a PlayerAdded function. The first loop works, the second loop doesn’t work and I am not getting errors. Does someone know why it doesn’t work? And how can I fix this?
local function ChangeWalkSpeed(Player, NewWalkSpeed)
if Player and Player.Character then
if Player.Character:FindFirstChildWhichIsA("Humanoid") then
Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = NewWalkSpeed
end
end
end
game.Players.PlayerAdded:connect(function(Player)
local Lead = Instance.new("Folder")
Lead.Name = "hiddenstats"
Lead.Parent = Player
local RebirthMultipler = Instance.new("NumberValue",Lead)
RebirthMultipler.Name = "RebirthMultipler"
RebirthMultipler.Value = 1
RebirthMultipler.Parent = Lead
local RebirthMultipler = Instance.new("IntValue",Lead)
RebirthMultipler.Name = "RebirthMultiplerTime"
RebirthMultipler.Value = 0
RebirthMultipler.Parent = Lead
local RebirthMultipler = Instance.new("IntValue",Lead)
RebirthMultipler.Name = "SpeedMultiplerTime"
RebirthMultipler.Value = 0
RebirthMultipler.Parent = Lead
wait(14)
local a = Player.hiddenstats.RebirthMultiplerTime
local b = Player.hiddenstats.SpeedMultiplerTime
local rebm = Player.hiddenstats.RebirthMultipler
while wait(1)do -- this works --
if b.Value >0 then
a.Value = a.Value -1
rebm.Value = 2
else
a.Value = a.Value +1
a.Value = a.Value - 1
rebm.Value = 1
end
end
while wait(1)do -- this doesn't work--
if b.Value >0 then
b.Value = b.Value -1
ChangeWalkSpeed(Player, 32)
else
b.Value = b.Value +1
b.Value = b.Value - 1
ChangeWalkSpeed(Player, 16)
end
end
end)
Well your issue is that the loop will not run because the code is already running another loop, you could use coroutines to put these loops in separate threads so they work independently
local function ChangeWalkSpeed(Player, NewWalkSpeed)
if Player and Player.Character then
if Player.Character:FindFirstChildWhichIsA("Humanoid") then
Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = NewWalkSpeed
end
end
end
game.Players.PlayerAdded:connect(function(Player)
local Lead = Instance.new("Folder")
Lead.Name = "hiddenstats"
Lead.Parent = Player
local RebirthMultipler = Instance.new("NumberValue",Lead)
RebirthMultipler.Name = "RebirthMultipler"
RebirthMultipler.Value = 1
RebirthMultipler.Parent = Lead
local RebirthMultipler = Instance.new("IntValue",Lead)
RebirthMultipler.Name = "RebirthMultiplerTime"
RebirthMultipler.Value = 0
RebirthMultipler.Parent = Lead
local RebirthMultipler = Instance.new("IntValue",Lead)
RebirthMultipler.Name = "SpeedMultiplerTime"
RebirthMultipler.Value = 0
RebirthMultipler.Parent = Lead
wait(14)
local a = Player.hiddenstats.RebirthMultiplerTime
local b = Player.hiddenstats.SpeedMultiplerTime
local rebm = Player.hiddenstats.RebirthMultipler
coroutine.wrap(function()
while wait(1)do -- this works --
if b.Value >0 then
a.Value = a.Value -1
rebm.Value = 2
else
a.Value = a.Value +1
a.Value = a.Value - 1
rebm.Value = 1
end
end
end)()
coroutine.wrap(function()
while wait(1)do -- this doesn't work--
if b.Value >0 then
b.Value = b.Value -1
ChangeWalkSpeed(Player, 32)
else
b.Value = b.Value +1
b.Value = b.Value - 1
ChangeWalkSpeed(Player, 16)
end
end
end)()
end)
I wouldn’t trust spawn due to how execution can take longer for it to run depending on conditions like frame rate and throttling conditions, as it runs the next time roblox runs an update cycle, which basically acts like a wait(). Coroutines from my information don’t have this issue and are more preferred in general to use over spawn, but to each their own of course
I don’t believe there is a hard coded limit for the maximum amount of coroutines but it will eventually fail (if you make hundreds of them) but you really shouldn’t use it like this. It is always better if you use bindable or remote events to let the server know when your value has changed rather than constantly checking.