Using range for Key Press

I am trying to make a function that will do something when a player presses E within 15 studs. I am doing this in a local script as the function will Fire an Event to the server later on. I am trying to use .Magnitude but I am not understanding it very well. I am having an issue with detecting distance between Player and Part.

Heres my Code in LocalScript.

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		local distanceX = (Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").Position.X - Part.Position.X).Magnitude
		local distanceZ = (Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").Position.Z - Part.Position.Z).Magnitude
		if distanceX <= 15 and distanceZ <= 15 then
			GUI.Visible = true
			wait(1)
			GUI.Visible = false
		end
	end
end)

Try to not do z and x and just try doing:


		local distance= (Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").Position- Part.Position.)Magnitude
		if distance <= 15 then

1 Like

It isn’t working for me. I’m not sure why not but it will not detect whether I am in range or not.

Hmm, check what the distance of the part is by printing the distance. Also, are you getting any errors?

You may want to consider using a ProximityPrompt. You can customize the range of the prompt so that it will only work if the player is within a certain distance from the Part. You can just insert a ProximityPrompt into the Part and then put this in your LocalScript:

Part.ProximityPrompt.Triggered:Connect(function(player)
--do whatever you want here when the player activates the Prompt. 
end)
2 Likes

Its in a LocalScript so I can not print.

Yeah im pretty sure you can print in local scripts. Where is the local script in?

The LocalScript is in StarterGui. I looked it up and it mentioned that you can not print in LocalScripts but I could just not be remembering correctly.

Hmm, that’s strange. Because printing in local scripts was always possible and I always do it for debugging. Maybe your script is just not running at all?

It is running for sure as other parts of it are working from a OnClientEvent section but I am going to try Proximity Prompts and see if that helps at all.

1 Like

Do I still need to use UserInputService for ProximityPrompt?

Nope, proximity prompt is a instance that you can create inside a part

1 Like

No. The ProximityPrompt does all of that for you. You also don’t have to do the range checks either since you can set the range for the ProximityPrompt in the Properties window in Studio.

1 Like

Would I be able to connect a ProximityPrompt to a player? Would I use Instance.new or something of that sort?

Yeah!

Script in server storage


game.Players.PlayerAdded:Connect(function(plr)

local Prompt = instance.new(“ProximityPrompt”)

—change the prompts settings

Prompt.Parent = plr.Character:FindFirstChild(“HumanoidRootPart”)
end)





1 Like

Do you mean creating a ProximityPrompt that is always attached to the player’s character and other players can interact with it?

If so, then the answer is yes. I have done that in a couple of my projects before. You can create a ProximityPrompt using Instance.new and parent it to the player’s HumanoidRootPart.

1 Like

Is there any way I could make this ProximityPrompt available to only certain players without running remote events back and forth between scripts?

Ok great I will see if I can fit this to my specific situation. Thank you so much.

1 Like

Also about this, I don’t think so. If you want there to be a proximity prompt only visible to 1 player, your gonna need to add it locally (or just enable it) then fire a remote event from there when it’s activated to the server and do what you want there

Do you think creating the Proximity Prompt in the LocalScript for the player that can see it would work? I would then FireServer to do said action.