Reduce local player walkspeed through Script

Hi, I am currently creating a Hunger System. I want there to be some sort of effects once the players ate the food

tool = script.Parent
players = tool.Parent
eated = false
canvalue = 10
local Plr = game:GetService("Players").LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Hum = Char:WaitForChild("Humanoid")

playerEat = game:GetService("ReplicatedStorage").Eat

game.Players.PlayerAdded:Connect(function(player)
	players = player
end)

tool.Activated:Connect(function()	
	playerEat:Fire(players,canvalue)
	Hum.WalkSpeed = 2
	eated = true
	if eated == true then
		script.Parent:Destroy()
	end
end)

As you can see here, I am trying to make it so If the tool is activated then the local player character speed needs to be lowered. But since It is a script, It gave me this error :

Workspace.Can.Script:6: attempt to index nil with 'Character'

I am sorry If this is not clear enough but if it isn’t please feel free to ask me to reword this question in a way where you could understand it

1 Like

try just

Plr.Character

instead of

Plr.Character or Plr.CharacterAdded:Wait()
1 Like

Are you sure this is a localscript? You tried to get Players.LocalPlayer, which is only accessible via a localscript, but from your error I could tell that LocalPlayer was nil. If you need to do this from a ServerScript inside of the tool, make sure the ServerScript is a direct child of the tool, then get the character with Script.Parent.Parent (Tools are children of the character when held).

2 Likes

Still got the same error

1 Like

It is a script, mentioned in the title. Thanks for the suggestion, I will try it out and let you know if it works

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.