I am trying to make a script in Roblox so that if you are 20 studs away and you press E it will bring up an NPC Dialog. It is being run inside a LocalScript. For the moment I just have it displaying a message when you press E, however it won't display the message.
local HumanoidRootPart = game.Players.LocalPlayer:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")
local part = game.workspace.TableBox.TableTop
UserInputService.InputBegan:connect(function(keyCode)
if keyCode.keyCode == Enum.KeyCode.E then
if (part.Position - HumanoidRootPart.Position).magnitude < 20 then
print("E has been pressed")
end
end
end)
I am also getting this error in the output(in orange):
Infinite yield possible on 'Players.icrann:WaitForChild("HumanoidRootPart")'
I expect local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
to work, but it ends up bring up the error:
Players.icrann.PlayerScripts.Script:1: attempt to index field 'Character' (a nil value)
And also when I play the game my character in the explorer looks like this:
local HumanoidRootPart = game:GetService("Players").LocalPlayer:WaitForChild("HumanoidRootPart") --It is .LocalPlayer, cuz localplayer is the player on that client
local UserInputService = game:GetService("UserInputService")
local part = workspace.TableBox.TableTop
UserInputService.InputBegan:connect(function(keyCode)
print(HumanoidRootPart.Position)
if keyCode.keyCode == Enum.KeyCode.E and (part.Position - HumanoidRootPart.Position).magnitude <= 20 then
print("E has been pressed")
end
end)