I have a script that when a player touche a brick a infos Gui appear, but when i close this frame the gui appear once again because the player still in the part when he leaves the Infos box. How can i do that when a player hit the detector block this one change his position wo when the player leave the infos box he don’t have to close again the frame.
script.Parent = script.Parent
local event = workspace.Open
local part = workspace.NewsBox
part.Touched:connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
event:FireClient(plr)
end
end)
local frame = script.Parent
local button = frame.Close
local open = frame.Open.Value
local event = workspace.Open
event.OnClientEvent:Connect(function()
if not open then
frame.Visible = true
open = true
end
end)
button.MouseButton1Click:Connect(function()
if open then
frame.Visible = false
open = false
end
end)
You would need have a debounce, but a simple boolean debounce would not work. Instead, you would use a table and insert the names of the player in the table once they are on the part. When the button is pressed to close the frame, fire a remote event to the server wait a few seconds and remove them from the table.
Your detector script should look like this:
script.Parent = script.Parent
local event = workspace.Open
local part = workspace.NewsBox
local PlayersTouching = {}
part.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and not table.find(PlayersTouching, plr.Name) then
table.insert(PlayersTouching, plr.Name)
event:FireClient(plr)
end
end)
event.OnServerEvent:Connect(function(plr)
local index = table.find(PlayersTouching, plr.Name)
if index then
wait(2) -- After 2 seconds, they can use the part again. This gives them enough time to walk off of it.
table.remove(PlayersTouching, index)
end
end)
And your frame LocalScript should look something like this:
local frame = script.Parent
local button = frame.Close
local open = frame.Open.Value
local event = workspace.Open
event.OnClientEvent:Connect(function()
if not open then
frame.Visible = true
open = true
end
end)
button.MouseButton1Click:Connect(function()
if open then
frame.Visible = false
open = false
event:FireServer()
end
end)