I try to get the player, but this is all that comes up.
Workspace.speedGiver.speedGiverScript:4: attempt to index local 'playr' (a nil value)
`local Players = game:GetService("Players") local playr = Players.LocalPlayer local character = playr.Character local humanoid = character.Humanoid local function onTouch() humanoid.Walkspeed = 22 end onTouch()```
Also, sorry I have no idea how to add the code the right way.
okok so you're not gonna believe this but server scripts are not the same as localscripts. they cannot access the LocalPlayer, nor can they access the PlayerGui (rather, any client-sided descendants of a player.)
first, you should connect to the touched event to the script's parent, like this:
script.Parent.Touched:Connect(function(t) end);
we place an anonymus function within the connect (which is cool fancy speak for creating the function in the event) and it calls the function within every contact
now, we use :FindFirstAncestorWhichIsA() and Players:GetPlayerFromCharacter(). we use FindFirstAncestorWhichIsA incase you have any nested parts, like so:
script.Parent.Touched:Connect(function(t) if (t:FindFirstAncestorWhichIsA('Model')) then end end);
we can have two if statements, but we can be cool kids and have ONE! (and also use variables cuz cool kids use those too)
script.Parent.Touched:Connect(function(t) local Ancestor = t:FindFirstAncestorWhichIsA('Model'); if (Ancestor and game:GetService('Players'):GetPlayerFromCharacter(Ancestor)) then local Humanoid = Ancestor:FindFirstChild('Humanoid', true) if (Humanoid) then Humanoid.WalkSpeed = 25; end end; end);
and zoom bop bap BOW you got your code thing. if you wish, you can use a table-based debounce to make sure that other people can touch this at once, or you can use a bool-based debounce to make sure only one person can touch this at a time.
woot poot, enjoy stuff!
You need to use the Touched event with the part you mentioned, then you need to use the GetPlayerFromCharacter() with hit.Parent to get the player.
script inside the part will look like this
script.Parent.Touched:Connect(function(hit) pcall(function() hit.Parent.Humanoid.Walkspeed = 22 end) end)
When something touches it, if it is from the player, then it will find the humanoid and increase the walkspeed. The pcall will deal with any errors given from other parts touching it