Need help with infinite loop

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)

I didnt want it to immediately close if it finds GiveUI. Because i have a button to close it. If player does not close it 15 secs i wanted it to close but ok. I’ll try thanks.

oh ok if you need it let me add it back in

post should be edited soon

Ah. Okay thanks. I understood now. I didnt need wait time bc i already have script that closes the gui when player leaves the area. Thanks again a lot!

1 Like