0

I am currently making a script so that when a block is touched, the player will go to position. It is

game.Workspace.AUPortal.Glitch4.Position

and me need help in this error.

Workspace.A12.MovePlayer:4: attempt to index nil with 'WaitForChild'

Script:

function onTouched(hit)
 local h = hit.Parent:findFirstChild("Humanoid")
 local playerMod = require(game:GetService("Players").LocalPlayer:WaitForChild("PlayerModule"))
 local controls = playerMod:GetControls()
 if h~=nil then
 controls:Disable()
 pos = game.Workspace.AUPortal.Glitch4.Position 
 h:MoveTo(pos)
 wait()
 end
end
script.Parent.Touched:connect(onTouched)
Egor Skriptunoff
23.8k2 gold badges38 silver badges68 bronze badges
asked Feb 23, 2022 at 9:27

1 Answer 1

0

I can only assume you are trying to access the LocalPlayer inside of a standard Script which is something you cannot do.

You can only access the LocalPlayer inside of a LocalScript, hence why you're getting an error for the WaitForChild. Because the LocalPlayer does not exist (it's nil).

With the Touched event you can get actually get a reference to the player you're trying use:

Part.Touched:Connect(function(HitPart)
 local Humanoid = HitPart.Parent:FindFirstChild('Humanoid');
 if (Humanoid) then
 local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent);
 -- You can then use this player reference.
 end
end)
answered Feb 23, 2022 at 9:40
Sign up to request clarification or add additional context in comments.

2 Comments

I agree, that LocalPlayer = nill. But. I solved the problem with only null value. Now this local player = game.Players:GetPlayerFromCharacter(HitPart.Parent) write me error about: GetControls is not a valid member of Player "Players.DanilX448" (This function doesn't exist)
GetControls is a clientside function. You would need to do this in a LocalScript or use a RemoteEvent, alternatively you could just anchor the Humanoid for the short period of time that you don't want them to move.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.