Looping this Check

How do I loop this Check? The script is a ServerScript, and player is defined.

local playerGui = player:WaitForChild("PlayerGui")
local countdownVal = playerGui:WaitForChild("Emotes").Holder.countDown

countdownVal.Changed:Connect(function()
	if countdownVal.Value == 0 then
		emoteEvent:FireAllClients(player)
		print("Fired event to "..player.Name)
	end
end)
1 Like

Is that entire script? cant really understand what you want to get.
Also, you putting player into FireAllClient which doesn`t take player as argument because its firing to all players

1 Like

Somewhat. Basically I want to loop this check because I’m making a reward system. When the countdown reaches zero, it fires an event. I want it to repeat that process until the player leaves.

1 Like

What would you like to loop though?
Would you like to loop the emoteEvent:FireAllClients(player) or just check if the countdown value reached 0?

1 Like

The countdown value, the reward system is based on a timer.

1 Like

Well you could try this but just what HafuPlay said, It’s hard to understand what you want to achieve.
Maybe a clearer explanation would help?

local playerGui = player:WaitForChild("PlayerGui")
local countdownVal = playerGui:WaitForChild("Emotes").Holder.countDown

local function CheckCountdown()
    while countdownVal.Value > 0 do
        wait(1)
    end

    -- The countdown reached zero, trigger the reward system here or add more code
    emoteEvent:FireAllClients(player)
    print("Fired event to " .. player.Name)
end

spawn(CheckCountdown)

1 Like

The countDown value counts down all the way to zero, and when this happens, it rewards a player. The countDown value repeats (in a separate script), and the check loops again to see if countDown has reached zero again.

1 Like

Based on your explanation, my code might work. Give it a try

1 Like

The loop doesn’t seem to work. Checked the print and it only printed the instance (Fired to player.Name) once and one time only.

Can i see your countdown script?
Could you provide all the code which is related to the reward system?

This separate thread contains all the code.

Okay noted, Thank you.

Maybe try this code instead?

local playerGui = player:WaitForChild("PlayerGui")
local countdownVal = playerGui:WaitForChild("Emotes").Holder.countDown
local given = false

spawn(function()
    while wait() do
        if countdownVal.Value == 0 and not given then
            emoteEvent:FireAllClients(player)
            print("Fired event to " .. player.Name)
            given = true 
        elseif countdownVal.Value > 0 then
            given = false 
        end
    end
end)

Should I put this in the onPlayerJoin?
EDIT: Just did, unfortunately it doesn’t work.

No, just copying the code should work.
Reaction to your EDIT: Any errors or something like that? Are you sure the timer counts down to 0 at all?
Maybe add some debug prints like a print(countdownVal.Value)

1 Like

I see the problem. The value of the countVal doesn’t update.
EDIT: Nevermind, got it working. The only problem is that the reward script you sent doesn’t print again after it reaches zero.

game.Players.PlayerAdded:Connect(function(player)
	local playerGui = player:WaitForChild("PlayerGui")
	local countdownVal = playerGui:WaitForChild("Emotes").Holder.countDown
	local given = false

	while task.wait() do
		if countdownVal.Value == 0 and not given then
			given = true
			emoteEvent:FireClient(player)
			print("Fired event to "..player.Name)
		else
			given = false
		end
	end
end)

It just spams the print for no reason.
EDIT: Fixed it, now for the actual reward itself.

Glad that you got it fixed :+1:
Goodluck with your game!

1 Like

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