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?
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.
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
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
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.
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
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