✓ Accepted answer
Judging by your use of script.Parent.Back.Position I assume that your LocalScript is within workspace. This would be your issue as LocalScripts cannot execute in any context outside of the Backpack, Character, PlayerGui, PlayerScripts, or the ReplicatedFirst service.
To fix this issue, simply put your script in something like StarterCharacterScripts and reference the object in a different way, like so:
(Also, a magnitude check is not needed if you are getting the distance from a player's character, as you can use the DistanceFromCharacter method of the player)
local Back =
local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
UIS.InputBegan:Connect(function(input, GPE)
if GPE then return end
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
if plr:DistanceFromCharacter(Back.Position) <= 20 then
print("yes")
end
end
end
end)
As a couple pointers, I've added a GameProcessedEvent check on line 7; this is entirely optional, but recommended as the event could potentially fire while the player is chatting if this check did not exist.
I've also added a UserInputType check on line 9 to prevent any errors from appearing regarding the KeyCode being nil for other types of inputs.
3 comments
.Magnitude maybe idk xd
that should have worked
I had it in workspace