So i have this script, and its meant to run forever. But it stops running whenever the player dies
I think its because i have it inside StarterPlayerScripts, but where else should i put it?
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local gui = plr.PlayerGui.ValueGui
local textLabel = gui.SpeedValue
while true do
wait(5)
repeat wait() until hum ~= nil
hum.WalkSpeed = hum.WalkSpeed + 1
textLabel.Text = "Speed: "..hum.WalkSpeed
end
The issue is you repeat waiting for hum to not be nil, but after the character has died you never actually search for a new humanoid again…
Try changing the repeat loop to:
repeat
wait()
if not char or not char:IsDescendantOf(game.Workspace) then
char = plr.Character or plr.CharacterAdded:Wait()
end
hum = char:FindFirstChild("Humanoid")
until hum ~= nil
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local gui = plr.PlayerGui.ValueGui
local textLabel = gui.SpeedValue
while true do
wait(5)
repeat
wait()
if not char or not char:IsDescendantOf(game.Workspace) then
char = plr.Character or old.CharacterAdded:Wait()
end
hum = char:FindFirstChild("Humanoid")
until hum ~= nil
hum.WalkSpeed = hum.WalkSpeed + 1
textLabel.Text = "Speed: "..hum.WalkSpeed
end
Does it not increase the actual walkspeed or does it just not update the GUI? You can check by looking at the actual Humanoid in Studio.
There may be a secondary issue linked to the other replier’s suggestion regarding the GUI itself - it needs ResetOnSpawn to be false otherwise this script will end up referencing an old version of the GUI that has been destroyed and not the newly spawned version.
Having ResetOnSpawn false means the same GUI remains regardless of resets.
It doenst give you any speed when you join the game, but first when you die the first time, so the script only starts running when you have died your first time
This could be due to cases where the character is already loaded when the script begins running.
I’ve tweaked a couple bits below to move the character logic entirely into the loop to ensure it doesn’t do any strange yielding at the start and to make it clear that it gets the new character if there isn’t one.
Hopefully this solves the issues for you.
local plr = game.Players.LocalPlayer
local gui = plr.PlayerGui:WaitForChild("ValueGui")
local textLabel = gui.SpeedValue
local char, hum
while true do
wait(5)
repeat
wait()
if not char or not char:IsDescendantOf(game.Workspace) then
char = plr.Character or plr.CharacterAdded:Wait()
end
hum = char:FindFirstChild("Humanoid")
until hum ~= nil
hum.WalkSpeed = hum.WalkSpeed + 1
textLabel.Text = "Speed: "..hum.WalkSpeed
end
Out of curiousity, why are you using repeat wait() until hum ~= nil?
In all honesty I’d just use Player.CharacterAdded and just increase the WalkSpeed of the Humanoid from there.
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
while Humanoid.Health > 0 do
Humanoid.WalkSpeed = Humanoid.WalkSpeed + 1
wait(5)
end
end)
player.CharacterAdded:Wait() only runs the code the first time the character is added. You need to run the code every time the character is added so that the script doesn’t break when the character respawns.
Code:
local plr = game.Players.LocalPlayer
local gui = plr.PlayerGui.ValueGui
local textLabel = gui.SpeedValue
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
while wait(5) do
if hum.Health == 0 then break end
hum.WalkSpeed = hum.WalkSpeed + 1
textLabel.Text = "Speed: "..hum.WalkSpeed
end
end)