Need help with infinite loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want my fuel gui to close whenever player leaves the area
  2. What is the issue? Include screenshots / videos if possible!

    Infinite loop
  3. 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.

2 Likes

Question, What do you mean by infinite loop?

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”

1 Like

1 Like

I think the problem is that the connection gets made every time the part is touched

making thousands of connections for a TouchEnded

So how could i fix this? Any clues?

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)

code is here

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.

image

(Script 2)

Only make those two events in one script.

1 Like

if player is nil then this line will error

Make it this:


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)

1 Like

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)

Oh, I forgot. My bad hehe, Let me fix it up!

I added the indentation afterwords in an edit btw

Thanks for whose trying to help me. But i have another thing i think can be considered as loop


image

you aren’t debouncing properly
also you need to understand what a loop is
image

your code runs multiple times because you are touching it multiple times

make sure to use debounce in your if statements!

Thanks. I already tried adding debounce == false in the red spot.


and now i added debounce in if(plr) and it works fine i guess… Thanks!

could you send the code, I want to remove some unnecessary code for you

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

also I didn’t think you needed that wait(15)