How to Stop Overlapping While True Do's?

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 am making a tag game where someone touches a golden ball to turn their character gold. While the person is gold, they have a Time leaderstat that increases, and the player with the most Time at the end will win the round. If the player is not gold, the Time leaderstat should tick down at a slower rate, so other players can catch up. The player loses their gold color if they are tagged by another player (who takes the gold color, but I have not coded that yet,) or if they are killed.

  1. What is the issue? Include screenshots / videos if possible!

The time is successfully able to tick up and down at the two rates, but if the player dies multiple times the subtract condition applies twice, so instead of going down 1 time every 3 seconds, it plays another subtraction loop on top of the first.

local adding = false
local deb = false
local repstor = game:GetService("ReplicatedStorage")
local soundoff = repstor.SoundActivate
local timeV = 1

game.Workspace.Goldball.Touched:Connect(function(grabber)
	--print("running Grab")
	if grabber.Parent:FindFirstChild("Humanoid") and grabber.Parent:FindFirstChild("Humanoid").Health > 0 and deb == false then
		soundoff:FireAllClients()
		deb = true
		--print("Is Human")
		local highlight = Instance.new("Highlight", grabber.Parent) -- Set the character variable to the character model of the targeted player
		highlight.Adornee = grabber.Parent
		highlight.FillColor = Color3.new(1, 0.788235, 0.14902)
		highlight.FillTransparency = .2
		highlight.OutlineColor = Color3.new(1, 0.976471, 0.294118)
		grabber.Parent:SetAttribute("Midas", true)
		adding = true
		game.Workspace.Goldball.Transparency = 1
		game.Workspace.Goldball.Highlight.OutlineTransparency = 1
		--script.Parent.SelectionSphere.Transparency = 1
		game.Workspace.Goldball.CanTouch = false	


	end
	wait(.25)

	deb = false
end)



game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		Char.Humanoid.Died:Connect(function()
			--do code
			print(Char.Name .. " has died")
			if Char:GetAttribute("Midas") == true then
				Char:SetAttribute("Midas", false)
				Char.Highlight:remove()
				adding = false
				game.Workspace.Goldball.CanTouch = true
				game.Workspace.Goldball.Transparency = .1
				game.Workspace.Goldball.Highlight.OutlineTransparency = 0
			end
		end)
		
		while true do
			wait(timeV)
			if Char:GetAttribute("Midas") == true and adding == true then
				Player.leaderstats.Time.Value += 1
				timeV = 1
			end
			if Char:GetAttribute("Midas") == false and adding == false and Player.leaderstats.Time.Value > 0 then
				Player.leaderstats.Time.Value -= 1
				timeV = 3
			end
		end
	end)
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried changing the position of the while true do, and making them both run on the same 1 second clock, but ideally I want the tick down to be slower than the tick up. Any suggestions?

You just need to add a condition to stop your loop. e.g. while Char.Parent do

I’ve tried while Char.Parent and while Player and both of them break the script. It no longer adds points. I am assuming it plays multiple times because the function loads whenever the character is added.

I fixed it by binding the subtraction to the character added portion, then adding another variable that would switch whether or not it applies, but I am planning on remaking this system using remote events, so it no longer applies.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.