While true and (variable) == (boolean) do not obeying

Im making a kind of debounce thing to my code so it doesnt run twice when its not supposed to. I use the debounce like

local db = false
while true and db == false do
task.wait(1)
(code)
db = true
end

Im using a while true do loop for doing alot of if statements every second until the debounce is true. I can see that the debounce is equal to true but it still runs. Ive searched for quite a while but no result. Thanks.

in this case you can do

local db = false
repeat task.wait(1)
-- code here
 until db
1 Like

I tried this but, no effect. Heres the part of the code if you need it.

local db = false
repeat task.wait(2)
	if game.Workspace:FindFirstChild("clone") then
		local distance = game.Players.LocalPlayer:DistanceFromCharacter(game.Workspace:WaitForChild("clone").Position)
		if distance <= 50 then
			local db = true
			print(db)
			local playerpos = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
			game.Workspace.deathbox:FireServer(playerpos)
		end
	end
	until db

The issue is this:

You are setting it as a local variable, rather than replacing the global one. Remove local and try again.

2 Likes

this should be the correct reason;

also @BorisGlibusic here is your code cleaned up:

local player = game.Players.LocalPlayer
local db = false
repeat task.wait(2)
	if not workspace:FindFirstChild("clone") then
		continue
	end

	if player:DistanceFromCharacter(workspace.Clone.Position) <= 50 then
		db = true
		workspace.deathbox:FireServer(player.Character.HumanoidRootPart.Position)
	end

	until db

on the side note:

  • do not put remote events under workspace (its unorganized) rather, make a folder under replicated storage
  • using a remote event for this leaves a vulnerability where an exploiter could fire that remote abunch of times
1 Like