Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Make character speed equal to leaderstat?

So, I have a leaderstat called "Speed" and I want the player's character's speed to be equal to the players leaderstat plus the original (16)
EX: Leaderstats Speed = 23 then character speed = 39 Here's what I have, it definently does not work (Localscript in StarterCharacterScripts):

local plr = game.Players.LocalPlayer

plr.leaderstats.Speed.Changed(function()
	wait(0.05)
	local char = plr.Character
	local Lname = char.Name
	local h = char.Humanoid
	local tt = h.WalkSpeed - h.WalkSpeed
	local gg = plr.leaderstats.Speed
	
	while true do
	h.WalkSpeed = tt + gg
	end
end)

4 comments

0

You are checking an infinite times a second. When will that second pass?

0

You realise your 'tt' variable is going to be equal to 0 right?

0

i think originally i wanted player walkspeed to be equal to just their speed, not their speed plus their leaderstat

0

hiimgoodpack, so how would i fix that

3 answers

✓ Accepted answer

If it dont work PM on Roblox

First of all you have called the event wrong. To call an event you do this:

script.Parent.Touched:Connect(function()
end)

.Touched is the event, and :Connect connects the event to the function.

Secondly, you are calling an event that will most likely never happen. You are trying to change the players speed in the event speed.Changed. Also, in a while true do loop, you MUST have a wait() otherwise the game will just crash.

local player = game:GetService("Players").LocalPlayer --gets player
local char = player.Character or player.CharacterAdded:Wait()--gets character
local humanoid = char:FindFirstChild("Humanoid")--gets the humanoid

local leaderstats = player:FindFirstChild("leaderstats") --gets leaderstats
local speed=  leaderstats:FindFirstChild("Speed") --gets the speed



humanoid.WalkSpeed = humanoid.WalkSpeed + speed.Value
--this will get the humanoid, then the humanoids speed, and add speeds value to it
--make sure 'speed' actually has a value

3 comments

0

What are you doing? Dissembling your computer and touching the bytes that make up the value object?

0

doesnt work

0

did u put this LocalScript in PlayerCharacterScripts?

Defining variables.

local player = game:GetService("Players").LocalPlayer
local char = player.Character

Waits for the players Speed value in leaderstats to change.

player.leaderstats.Speed.Changed(function()

Once it is changed, it will set their WalkSpeed to whatever their Speed leaderstat value is equal to, plus an additional 16.

	char.Humanoid.WalkSpeed = player.leaderstats.Speed.Value + 16
end)

Full Code;

local player = game:GetService("Players").LocalPlayer
local char = player.Character

player.leaderstats.Speed.Changed(function()
    char.Humanoid.WalkSpeed = player.leaderstats.Speed.Value + 16
end)

Hope I helped! Happy developing!

5 comments

0

Ive tampered and moved from local to reg script but no matter what it still shows the same error

0

"leaderstats is not a valid member of Player"

0

If you've changed it to a server script, you can't get the local player from it, only in studio. And you are not waiting for leaderstats to appear. Add a variable at the top of your script that has :WaitForChild("leaderstats")

0

doesnt work

0

Errors?

Basically it is detecting a change. If it hasn't changed then nothing happens So set it to it before it happens

local player = game.Players.LocalPlayer
local char = player.Character
	char.Humanoid.WalkSpeed = player.leaderstats.Speed.Value
	player.leaderstats.Speed.Changed(function()
    char.Humanoid.WalkSpeed = player.leaderstats.Speed.Value
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.