When the player is in range of the object, they are given a Gui prompting them to press "e"
When they press "e" It prints "It worked!" because that's what is in the Event script when the event is fired.
Layout of part that gets triggered magnitude. http://prntscr.com/6p5b1c
However, when they step into range of the object it does bring up the Gui, however if they leave... it doesn't go away and when they press "e" it still fires the remote event.
Local script Inside player gui
local interactionGui = game.ReplicatedStorage.InteractionGui
-- Define near, sets to nil
local near
local debounce = false
function update(player)
local mouse = game.Players.LocalPlayer:GetMouse()
local playerGui = player.PlayerGui
local hasGui = playerGui:FindFirstChild(interactionGui.Name)
local torso = player.Character.Torso
for _, interaction in pairs(interactions) do
if (interaction.Position - torso.Position).magnitude <= 3 then
near = interaction
mouse.KeyDown:connect(function(key)
if near and key == "e" and not debounce then
debounce = true
interaction.EventHandler.RemoteEvent:FireServer()
wait()
debounce = false
elseif not near and key "e" and not debounce then
debounce = true
print('You are not near target!')
end
end)
end
end
if near then
if not hasGui then
interactionGui:Clone().Parent = playerGui
end
elseif not near and hasGui then
hasGui:Destroy()
end
end
This is the script that functions when the event is fired. This script is located in the object that has been triggered magnitude on.
local event = script:WaitForChild("RemoteEvent") -- giving a name to the event so we can fire it specifically elsewhere
event.OnServerEvent:connect(function(key) -- define the function that will be called when the event is fired
print('It worked!')
-- this is where you put all the code you want to run when the event is fired
game.Workspace:WaitForChild("Baseplate").BrickColor = BrickColor.Random()
end)
The Entire Local script
local me = game:GetService("Players").LocalPlayer
local mouse = me:GetMouse()
-- REQUIRES: model and descendants of model can have :GetChildren() called
-- on them.
-- PURPOSE: Given a model, returns a list of all of the descendants of that model.
-- [If result is specified, it will add the descendants of result to model.]
function descendants( model, result )
result = result or {} -- No need to pass in result
for _, child in pairs(model:GetChildren()) do
table.insert(result, child) -- Much cleaner way to add to list
descendants(child, result)
end
return result -- Return the accumulated children
end
-- PURPOSE: Returns whether or not this part is an interaction site.
-- REQUIRES: part must be a ROBLOX object.
function isInteraction( part )
return part:IsA("BasePart") and part:FindFirstChild("Interaction") and part.Interaction:IsA("StringValue")
end
local interactions = {} -- List of all interaction sites.
local everything = descendants( workspace )
for _, object in pairs( everything ) do
if isInteraction( object ) then
table.insert( interactions, object )
end
end
-- Define the GUI we give to players
local interactionGui = game.ReplicatedStorage.InteractionGui
-- Define near, sets to nil
local near
local debounce = false
function update(player)
local mouse = game.Players.LocalPlayer:GetMouse()
local playerGui = player.PlayerGui
local hasGui = playerGui:FindFirstChild(interactionGui.Name)
local torso = player.Character.Torso
for _, interaction in pairs(interactions) do
if (interaction.Position - torso.Position).magnitude <= 3 then
near = interaction
mouse.KeyDown:connect(function(key)
if near and key == "e" and not debounce then
debounce = true
interaction.EventHandler.RemoteEvent:FireServer()
wait()
debounce = false
end
end)
end
end
if near then
if not hasGui then
interactionGui:Clone().Parent = playerGui
end
elseif not near and hasGui then
hasGui:Destroy()
end
end
-- Update all players
function updateAll()
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("Torso") and player:FindFirstChild("PlayerGui") then
update(player)
end
end
end
while wait(0.1) do
updateAll()
end
Uncopylocked place

2 comments
I believe I figured it out
Fixed it.