What solutions have you tried so far? Did you look for solutions on the Developer Hub?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I tried adding debounce but it didn’t to anything because it says attempt to index nil with playergui
-- This is an example Lua code block
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Your issue here is that TouchEnded acts mainly the same as the Touched event, you would most likely need to use Region3 to add players to a table when they are in the region and check every few seconds whether they are still in the Region or not.
If could also elaborate in what you mean by “infinite loop”
script.Parent.Touched:Connect(function(hit2)
local db = false
local rs = 2
script.Parent.TouchEnded:Connect(function(ontouchended)
local plr = game.Players:GetPlayerFromCharacter(hit2.Parent)
wait(1)
if(plr.PlayerGui:FindFirstChild("GiveUI")) then
db = true
plr.PlayerGui.GiveUI:Destroy()
wait(1)
db = false
wait(rs)
end
end)
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local playerGui = player.PlayerGui
if (playerGui:FindFirstChild("GiveUI")) then
local gui = playerGui:FindFirstChild("GiveUI")
if gui then
gui:Destroy()
end
end
end)
Make sure that this event is not inside another event.
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local playerGui = player.PlayerGui
if (playerGui:FindFirstChild("GiveUI")) then
local gui = playerGui:FindFirstChild("GiveUI")
if gui then
gui:Destroy()
end
end
end
end)
For example, And by the way this is the whole script that you desire. Hope it works!
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local giveUI = (the gui):Clone()
if player:FindFirstChild("GiveUI") then
return nil
else
giveUI.Parent = player.PlayerGui
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local playerGui = player.PlayerGui
if (playerGui:FindFirstChild("GiveUI")) then
local gui = playerGui:FindFirstChild("GiveUI")
if gui then
gui:Destroy()
end
end
end)
local debounce = false
local reset_secs= 2
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if(plr) and debounce == false then
if(plr.PlayerGui:FindFirstChild("GiveUI")) then
wait(15)
debounce = true
plr.PlayerGui.GiveUI:Destroy()
wait(reset_secs)
debounce = false
else
debounce=true
script.Parent.GiveUI:Clone().Parent = plr.PlayerGui
wait(reset_secs)
debounce=false
end
end
end)
local debounce = false
local reset_secs = 2
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if(plr) and debounce == false then
debounce = true
if(plr.PlayerGui:FindFirstChild("GiveUI")) then
task.wait(15)
plr.PlayerGui.GiveUI:Destroy()
else
script.Parent.GiveUI:Clone().Parent = plr.PlayerGui
end
task.wait(reset_secs)
debounce = false
end
end)
sorry for the wait, on a train and we were in a tunnel