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
http://www.roblox.com/games/233112602/Remote-Event-Testing
What is most likely happening is the event you connected has not been disconnected so it will still fire.
What you should do instead of connecting a new event/disconnecting it would probably be to have something similar to this:
mouse.KeyDown:connect(function(key) for _, interaction in pairs(interactions) do if (interaction.Position - torso.Position).magnitude <= 3 and key == "e" and debounce then debounce = true; interaction.EventHandler.RemoteEvent:FireServer(); wait(); debounce = false; end end end)
Edit: One second, looking at the place.
Edit2: This works:
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. function updateInteractions() local everything = descendants( workspace ) local t = { }; for _, object in pairs( everything ) do if isInteraction( object ) then table.insert( t, object ) end end interactions = t; end -- Define the GUI we give to players local interactionGui = game.ReplicatedStorage.InteractionGui -- Define near, sets to nil local distance = 7; -- how far a player can be from the part local player = game.Players.LocalPlayer; local mouse = game.Players.LocalPlayer:GetMouse() local playerGui = player.PlayerGui local hasGui = playerGui:FindFirstChild(interactionGui.Name) repeat wait() until player.Character; local torso = player.Character.Torso; local debounce = false local hasGui = false; function update() local near = false; for _, interaction in pairs(interactions) do if (interaction.Position - torso.Position).magnitude <= distance then near = true; break; end end if near and not hasGui then interactionGui:Clone().Parent = playerGui; hasGui = true; elseif not near and hasGui then if playerGui:FindFirstChild("InteractionGui") then playerGui.InteractionGui:Destroy(); hasGui = false; end end end mouse.KeyDown:connect(function(key) for _, interaction in pairs(interactions) do if (interaction.Position - torso.Position).magnitude <= distance and key == "e" and not debounce then print'passed' debounce = true; interaction.EventHandler.RemoteEvent:FireServer(); wait(); debounce = false; end 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 updateInteractions(); update(); end