Why does my code only work once?

Hello!
I have this problem with my code not working:

local Collect = game:GetService("CollectionService")
debounce = false

if debounce == false then
for i, Goal in pairs(Collect:GetTagged("Goals")) do
	debounce = true
	Goal.Touched:Connect(function(hit)

		local box = hit.Parent:FindFirstChild("Box")
		
		if box then

			game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)

				local hum = OnHit.Parent:FindFirstChild("Humanoid")
				local Player = game.Players:GetPlayerFromCharacter(OnHit.Parent)
				
				if Player then

					if Goal.Name == tostring(Player:FindFirstChild("leaderstats").Level.Value +1) then

						Player.leaderstats.Level.Value = Goal.Name

						box:Destroy()

                        debounce = false
					end
					
				end
				
				
			end)
			
			
		end
		
	end)
	
end
end

I have different levels where you have to push the box into the goals, the goals are tagged under Goals and my problem is that in the 1st level it works. It adds 1 to the leaderstats but it doesn’t work in the second level. I checked and I tagged every goal and I tried so long to fix it.
Does anyone know how to fix it?

any help is appreciated!

3 Likes

The issue is with your debounce variable and maybe touched connection

2 Likes

I tried it without the debounce and it still doesnt work and what is wrong with the touch connection?

1 Like

I think the problem is in this line:

if Goal.Name == tostring(Player:FindFirstChild("leaderstats").Level.Value +1) then

maybe try using else?

1 Like

How should I use else? or what should I put in it?

1 Like

try printing, if the goal name isn’t the same as Player:FindFirstChild("leaderstats").Level.Value +1, then i think you can figure it out better.

wait… it does print the goals name in the first level even tho it works there, but it doesnt print anything in the second level.

did it print what’s in the else?

yea in the first it printed it even tho it worked and it didnt print anything int he second level. the print is in the else. also when I spawn in the second level it works but for the 3rd level it doesnt work anymore. I think there is something wrong with the loop.

try this

local Collect = game:GetService("CollectionService")
debounce = false

for i, Goal in pairs(Collect:GetTagged("Goals")) do
	if not debounce then
        debounce = true

        Goal.Touched:Connect(function(hit)

		local box = hit.Parent:FindFirstChild("Box")
		
		if box then

			game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)

				local hum = OnHit.Parent:FindFirstChild("Humanoid")
				local Player = game.Players:GetPlayerFromCharacter(OnHit.Parent)
				
				if Player then

					if Goal.Name == tostring(Player:FindFirstChild("leaderstats").Level.Value +1) then

						Player.leaderstats.Level.Value = Goal.Name

						box:Destroy()

                        debounce = false
					end
					
				end
				
				
			end)
			
			
		end
		
	end)
end

i’m not in studio right now, so i can really do much, but try this.

Remade the debounce. If you have a working code for level 1 but it isn’t working for level 2 … I’d be looking hard at level 2’s set up. Maybe try running level 3 or 1 again. Just to see if this is where the problem is.

local Collect=game:GetService("CollectionService")
local db=true

for i, Goal in pairs(Collect:GetTagged("Goals")) do
	Goal.Touched:Connect(function(hit)
		local box=hit.Parent:FindFirstChild("Box")

		if box then
			game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)
				local hum=OnHit.Parent:FindFirstChild("Humanoid")
				local Player=game.Players:GetPlayerFromCharacter(OnHit.Parent)
				if Player and db then db=false
					if Goal.Name==tostring(Player:FindFirstChild("leaderstats").Level.Value +1) then
						Player.leaderstats.Level.Value=Goal.Name
						box:Destroy() 
					end task.wait(3) db=true
				end
			end)
		end
	end)
end

It doesnt work there was missing an end and even with it it still does not work

No, the Problem is on every next level for example: i spawn at level 2 and it works but it doesnt work on level 3 anymore, i spawn at level 4 and it works but it doesnt work on level 5 anymore. Also this code has the same issue

try this

local Collect = game:GetService("CollectionService")

for i, Goal in pairs(Collect:GetTagged("Goals")) do
    local debounce = false 

    Goal.Touched:Connect(function(hit)
        if debounce then return end
        debounce = true

        local box = hit.Parent:WaitForChild("Box")
        if box then
            game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)
                local hum = OnHit.Parent:WaitForChild("Humanoid")
                local Player = game.Players:GetPlayerFromCharacter(OnHit.Parent)

                if Player then
                    if Goal.Name == tostring(Player:WaitForChild("leaderstats").Level.Value + 1) then
                        Player:WaitForChild("leaderstats").Level.Value = Goal.Name
                        box:Destroy()
                        debounce = false 
                    end
                end
            end)
        end
    end)
end

the issue might be with the debounce variable. The debounce variable is set to false at the beginning of the script, but it is not reset to false after the first level is completed. This means that the code inside the if debounce == false statement will not run again for the second level.

it doesnt work it has the same problem

1 Like

Your debounce logic was incorrect, but there may also be an issue with the touched connection. I can’t give the exact code at the moment, but I can suggest things like this:

local Collect = game:GetService("CollectionService")

for i, Goal in pairs(Collect:GetTagged("Goals")) do
    local debounce = false
    local connection

    Goal.Touched:Connect(function(hit)
        if debounce then return end
        debounce = true

        local box = hit.Parent:WaitForChild("Box")
        if box then
            connection = game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)
                local hum = OnHit.Parent:WaitForChild("Humanoid")
                local Player = game.Players:GetPlayerFromCharacter(OnHit.Parent)

                if Player then
                    if Goal.Name == tostring(Player:WaitForChild("leaderstats").Level.Value + 1) then
                        Player:WaitForChild("leaderstats").Level.Value = Goal.Name
                        box:Destroy()
                        debounce = false
                        connection:Disconnect() -- Disconnect the connection
                    end
                end
            end)
        end
    end)
end

I tried it without any debounce but it has the same problem

The issue could also be due to the conversion of the Level.Value to a string using tostring and then comparing it with the Goal.Name.

if tonumber(Goal.Name) == Player:WaitForChild("leaderstats").Level.Value + 1 then
    Player:WaitForChild("leaderstats").Level.Value = tonumber(Goal.Name)
    box:Destroy()
    debounce = false
    connection:Disconnect() -- Disconnect the connection
end
1 Like