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)
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)
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.
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.
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.
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