Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I get errors while trying to change walk speed with the press of a button. Fix?

Asked by 4 years ago

I get this error, Workspace.BootShack.Boots.Clickbox.ClickDetector.BuyBoot:17: attempt to index nil with 'Name'

Code is right here.

01local player = game.Players.LocalPlayer -- this is the variable im referencing.
02 
03 
04script.Parent.MouseClick:Connect(function()
05 
06    if _G.points < 3 then
07 
08print("Player is missing required money.")
09 
10    end
11 
12    if _G.points > 2 then
13 
14            _G.points = _G.points - 3
15            workspace{player.Name}:FindFirstChild("Humanoid").WalkSpeed = 22 --I get the error right here.
View all 22 lines...
0
this a localscript? Pitched_mobile 191 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

if its a localscript just use localplayer

1if _G.points > 2 then
2_G.points = _G.points - 3
3game.Players.LocalPlayer.Character.Humanoid.WalkSpeed =22
4script.Parent.MaxActivationDistance = 0
5script.Parent.Parent.Parent.Boots.Transparency = 1
6script.Parent.Parent.Parent.Boots.CanCollide = false
7end
Ad
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
4 years ago
Edited 4 years ago

I'm sorry, but local scripts can't work on the workspace. Local scripts only work if it is a descendant of the local player or the local player's character. Instead, replace the local script with a server script (normal script) with the following code:

01script.Parent.MouseClick:Connect(function(playerWhoClicked) --calls a function when clickdetector is clicked
02    if _G.points < 3 then return end -- if player doesn't have enough money the script says screw it don't run this function
03    if _G.points > 2 then -- everything else after this line is the same as your script.
04        _G.points = _G.points - 3
05        local playerName = playerWhoClicked.Name
06        local chr = game.Workspace:FindFirstChild(""..playerName)
07        chr.Humanoid.Walkspeed = 22
08        script.Parent.MaxActivationDistance = 0
09        script.Parent.Parent.Parent.Boots.Transparency = 1
10        script.Parent.Parent.Parent.Boots.CanCollide = false
11    end
12end)

It should work now however if it doesn't work, let me know. I would use GetCharacterFromPlayer instead of FindFirstChild(""..playername) but sadly GetCharacterFromPlayer is not a thing in roblox scripting yet.

Answer this question