Like this? If so, it gives an error like this:
Oh itβs because I didnβt capitalize speed, so in the function put
(Line 8)
Player.Speed.Value = hum.WalkSpeed
Mb.
Player.leaderstats.Speed.Value = hum.WalkSpeed
Uh⦠So ⦠it is fixed) But the bug with the fact that at several deaths is no longer given 1 per second and 2 and so on. I mean, like this: the original player is given one per second speed. But if he dies for example 10 times, then he will actually be given 10 speeds per second.
Remove
spawn(function() end)
Also send a screenshot of when you fire addSpeed()
Um, I take it you wanted this screenshot?
If anything, I took out what you said, but it didnβt change anything.)
Try removing the character-added function. Along with the next few lines until line 29
If anything, I put everything back the way it was before the characteradded was removed
Alright, well I have to go but I wish you luck and I hope you figure it up.
Thank you) And good luck to you)
Every time addSpeed(hum)
is called, you spawn a loop that never ends. Even if the character dies, the previous call will still run, and the Humanoid instance it is referring to (the one before the Player respawns) still exists, just on the brink of being garbage collected.
Hereβs a fixed version of your code.
local plrs = game:GetService("Players")
plrs.PlayerAdded:Connect(function(plr)
warn("New player arrived:", plr.Name)
local ldrstats = Instance.new("Folder", plr)
ldrstats.Name = "leaderstats"
local speed = Instance.new("NumberValue", ldrstats)
speed.Name = "Speed"
plr.CharacterAdded:Connect(function(char)
local humanoid : Humanoid = char:WaitForChild("Humanoid")
task.wait(1) -- task.spawn() and task.wait() are the new spawn() and wait()
-- In every second, check if the Player's state is Dead (or informally, see if they died.)
while not (humanoid:GetState() == Enum.HumanoidStateType.Dead) do
local newSpeed = humanoid.WalkSpeed + 1
-- Update the walkspeed and the value at the same loop.
humanoid.WalkSpeed = newSpeed
speed.Value = newSpeed
task.wait(1)
end
end)
end)
Okay, I copied and pasted your script to replace the previous one, but now when a player dies his speed is reset to 16
and starts all over again)
Is the leaderboard used for displaying the highest walkspeed a player has achieved, or do you want the speed of the player to stay the same after they die?
I want the playerβs speed to be saved after death. For example, the player has a speed of 100, he died and his speed after death was 100 and continued to grow. That is, the current speed was saved
After respawning, get the walk speed value from the leaderstats, and apply that to the Humanoid. Oh yeah, the value should probably be initialized to 16 for the default walk speed.
This should save only for the running session. Once the player leaves, the βsavedβ data will be removed. See DataStoreService for saving and loading player data for future sessions.
local speed = Instance.new("NumberValue", ldrstats)
speed.Name = "Speed"
speed.Value = 16
plr.CharacterAdded:Connect(function(char)
local humanoid : Humanoid = char:WaitForChild("Humanoid")
humanoid.WalkSpeed = speed.Value
task.wait(1) -- task.spawn() and task.wait() are the new spawn() and wait()
-- In every second, check if the Player's state is Dead (or informally, see if they died.)
while not (humanoid:GetState() == Enum.HumanoidStateType.Dead) do
local newSpeed = humanoid.WalkSpeed + 1
-- Update the walkspeed and the value at the same loop.
humanoid.WalkSpeed = newSpeed
speed.Value = newSpeed
task.wait(1)
end
end)
Um⦠Do I have to replace the old script with this new one, or what?
The entire script should not be replaced. Just paste the code over the previous, relevant code that is the instancing of the speed object until the end of the plr.CharacterAdded:Connect(function(char)
function.
Add part of the line, like this:
local BaseSpeed = 16 -- It's outside of the Function.
if hum.Health <= 0 then
hum.WalkSpeed = BaseSpeed
break
end