Loop isn't registering global variable?

I’m trying to just tick a value down with a loop, and when the function is required again, an if statement prevents it from creating another loop, and instead, updates the variable ticking down in the loop back to full.

I just want it so that the loop could continue infinitely, given that the function is ran before it, since it’s supposed to ‘Repeat’ the loop while the value isn’t 0. This doesn’t happen, and the script just ignores the global variable and continues using its ‘own’ version of the variable.

local Countdown = 5	
        repeat
			wait(1)
				Countdown = Countdown-1
				print (Countdown)
		until Countdown == 0

In other words, it’s not recognizing the global variable, I don’t know why. I guess loops can’t do that? Does anyone know a workaround or something?

I’d really appreciate the help! :+1:

(This is how the bug looks like in printing)
Functions - Roblox Studio (gyazo.com)

Can you post a more complete version of the script, including the part that rases it back up

1 Like
function Module.InCombatActive(Player)
	-- Variables --
	local PlayerDataModule = require(game.ServerStorage.PlayerData)
	local UserID = Player.UserId
	local TweenService = game:GetService("TweenService")
	local Countdown = PlayerDataModule.PlayerDataStore[UserID].Countdown

	-- Script --

	if PlayerDataModule.PlayerDataStore[UserID].Combat == false then
		
			-- Updating the Player's Combat variable.
			PlayerDataModule.PlayerDataStore[UserID].Combat = true

			-- Updating the Player's GUI. (Activating IN-COMBAT)
			local CombatGUI = game.ServerStorage.GUI.InCombat:Clone()
			CombatGUI.Parent = Player.PlayerGui

				local Skull = CombatGUI.Skull
				local Text1 = CombatGUI.Skull.DangerLabel
				local Text2 = CombatGUI.Skull.TextLabel

				-- Setting up Skull Transparency Tween
				
				local SkullInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)
				
				local SkullTween = TweenService:Create(Skull,SkullInfo,{ImageTransparency = 0})

				-- Setting up Text Transparency Tween

				local TextInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)

				local TextTween = TweenService:Create(Text1,TextInfo,{TextTransparency = 0, TextStrokeTransparency = 0})
				local Text2Tween = TweenService:Create(Text2,TextInfo,{TextTransparency = 0, TextStrokeTransparency = 0.85})

				-- Playing the Tween.
				SkullTween:Play()
				TextTween:Play()
				wait(1)
				Text2Tween:Play()

			-- Time period before Combat is set back to false. If InCombatActive runs again, the time will be extended back to its maximum.			

				repeat
				-- Every second, the Countdown is lowered by 1.
				wait(1)
				Countdown = Countdown-1
				print (Countdown)
		until PlayerDataModule.PlayerDataStore[UserID].Countdown == 0

			-- Updating the Player's Combat variable & Destroying GUI.
			CombatGUI:Destroy()	
			PlayerDataModule.PlayerDataStore[UserID].Combat = false
			Countdown = 5

		elseif PlayerDataModule.PlayerDataStore[UserID].Combat == true then
			Countdown = 5
			
			print ("Countdown raised back up to . . ",Countdown)
		end
end

Here it is, the script is basically a combat tag script that is meant to reset the cooldown every time that they are struck. I still haven’t figured out the solution, can loops just not register global variables or am I doing something wrong :sweat_smile:

Someone please help bro :sweat_smile: :pray:

I’m still stuck with this problem, and I don’t know what’s going on

I believe the problem here is that you create a Countdown variable that is assigned the value of PlayerDataModule.PlayerDataStore[UserID].Countdown. Changing the variable Countdown does not change the value of PlayerDataModule.PlayerDataStore[UserID].Countdown. You then raised the variable to 5, and not the actual place that the variable inherited its value from.

Try this for your script:

ffunction Module.InCombatActive(Player)
	-- Variables --
	local PlayerDataModule = require(game.ServerStorage.PlayerData)
	local UserID = Player.UserId
	local TweenService = game:GetService("TweenService")
	-- Script --

	if PlayerDataModule.PlayerDataStore[UserID].Combat == false then

		-- Updating the Player's Combat variable.
		PlayerDataModule.PlayerDataStore[UserID].Combat = true

		-- Updating the Player's GUI. (Activating IN-COMBAT)
		local CombatGUI = game.ServerStorage.GUI.InCombat:Clone()
		CombatGUI.Parent = Player.PlayerGui

		local Skull = CombatGUI.Skull
		local Text1 = CombatGUI.Skull.DangerLabel
		local Text2 = CombatGUI.Skull.TextLabel

		-- Setting up Skull Transparency Tween

		local SkullInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)

		local SkullTween = TweenService:Create(Skull,SkullInfo,{ImageTransparency = 0})

		-- Setting up Text Transparency Tween

		local TextInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)

		local TextTween = TweenService:Create(Text1,TextInfo,{TextTransparency = 0, TextStrokeTransparency = 0})
		local Text2Tween = TweenService:Create(Text2,TextInfo,{TextTransparency = 0, TextStrokeTransparency = 0.85})

		-- Playing the Tween.
		SkullTween:Play()
		TextTween:Play()
		wait(1)
		Text2Tween:Play()

		-- Time period before Combat is set back to false. If InCombatActive runs again, the time will be extended back to its maximum.			

		repeat
			-- Every second, the Countdown is lowered by 1.
			wait(1)
			PlayerDataModule.PlayerDataStore[UserID].Countdown -= 1;
		until PlayerDataModule.PlayerDataStore[UserID].Countdown == 0

		-- Updating the Player's Combat variable & Destroying GUI.
		CombatGUI:Destroy()	
		PlayerDataModule.PlayerDataStore[UserID].Combat = false
		PlayerDataModule.PlayerDataStore[UserID].Countdown = 5

	elseif PlayerDataModule.PlayerDataStore[UserID].Combat == true then
		PlayerDataModule.PlayerDataStore[UserID].Countdown = 5

		print ("Countdown raised back up to . . ",PlayerDataModule.PlayerDataStore[UserID].Countdown)
	end
end
1 Like

pepo

Thank you bro. I’ll never forget this.

1 Like