How do I fix this? I'm pretty new to scripting and I'm trying to change the walkspeed of the player who touches the part. I've been trying to look for a way to see what refers to any player so that I can do things like change the walkspeed of anyone who touches the part which is what I try to do below. This is what I used to refer to any player in my script, not sure if this is how you do it though.
local player = game.Players.LocalPlayer
However when I ran this script which changes walkspeed this is the error I get, "attempt to index nil with 'Character'" I'm not completely sure what this means, but when I searched it up I found a suggestion to someone else with the same error to switch to local script (Random question: I thought that local scripts only work on things on the player and not inside the server that everyone can interact with, is that right?.) Local script did not give me an error, instead when I touched the brick nothing happened.
part = script.Parent local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() part.Touched:Connect(function(hit) if hit.Parent:FindFirstChildWhichIsA("Humanoid") then player.character.Humanoid.WalkSpeed = 150 end end)
Thank you for reading.
When setting the variable ‘players’, you’re not setting the function, but the return of ‘GetPlayers’ to that variable. This means that no matter how many times ‘players’ are referenced, the value will never change as it’s already been set, so get the service of players which is better
local player = game:GetService("Players")
About your script, you should make a global script, not a local script. You can access directly to the humanoid of the player instead of using ":FindFirstChildWhichIsA()" or use "FindFirstChild("Name")" or "IsA("Class")"
I have to tell you something every event passing information to the function that connected to. If an object hits the part the event passing it to the function which is "hit", you don't have to get it from out of the function (Variable(s)) if you already have it, unless you want to change all players' speed. So we say simply:
script.Parent.Touched:Connect(function(hit) local players = game:GetService("Players") --To get the service of players local playerHumanoid = hit.parent:WaitForChild("Humanoid") local hitfromPlayer = players:GetPlayerFromCharacter(hit.parent) --Getting the Player from its Character if hitfromPlayer then playerHumanoid.WalkSpeed = playerHumanoid.WalkSpeed + 5 end end)
Note: You can get the "Player" from its Character by using ":GetPlayerFromCharacter(Character)"