VIP Developer product timer help

I’ve been working on a VIP developer product (Much like the old vip in World // Zero) and came across a fork in the road.

Currently in my datastore, I have it so that when you click the purchase button and it goes through, The server fires to the client the amount that was purchased:

  game.ReplicatedStorage.RED.OnServerEvent:Connect(function(plr,VIPTime,Key) -- VIP giver

	local userdata = ServerUserData[plr.Name]
	--userdata.LastTimeCheck = os.time()
	if userdata.VipTime <= 0 then
		userdata.VipTime =  os.time() + VIPTime
		print(userdata.VipTime)
	    game.ReplicatedStorage.RED:FireClient(plr,userdata.VipTime,"VIPTimer")
	
	elseif userdata.VipTime < os.time() then
		userdata.VipTime = 0
		
	else
		userdata.VipTime = userdata.VipTime + VIPTime
		 game.ReplicatedStorage.RED:FireClient(plr,userdata.VipTime,"VIPTimer")
	end

	

end)

And this works fine, however, for my timer, I have a while loop, so when they buy more time in one sitting, the timer counts down their past time without the addition of their new time:

    local plr = game.Players.LocalPlayer


    local TimeScale = {
    	SECOND = 0,
    	MINUTE = 60,
    	HOUR = 3600,
    	DAY = 86400
    }

    game.ReplicatedStorage.RED.OnClientEvent:Connect(function(amnt,Key)
    	
    	
    	
    		if amnt > os.time() then
    		while wait(1) do
    			
    			local Amount =  amnt-os.time()
    			
    			local TimeInMinutes = math.floor(Amount/60)
    			local TimeInHours = math.floor(Amount/TimeScale.HOUR)
    			local Remainder = Amount % TimeScale.HOUR
    			
    			script.Parent.Text = TimeInHours..":"..Remainder % 60
    			
    			print (Amount)
    			if Amount <= 0 then Amount = 0
    				script.Parent.Text = "00:00"
    				print(tostring(plr).."has "..Amount.." of VIP time left!")
    				break
    			end
    			
    		end
    	end

    	

    		
    	
    end)

What would be the most logical way of adding time to the clock (in an Hour : Minute countdown )

Also, I’m having troubles with the actual clock itself, I dont really know how to display the amount of minutes. Is there a function like math.floor() but instead of rounding down it rounds up?

Thanks guys!

I’d probably suggest that you log the first time that the users purchases the timed product and have a separate variable that stores how much time the user has purchased(in seconds). Then, use some sort of loop to make sure that the user is within their time and display the timer. The opposite of math.floor() is math.ceil().

wow why does that make so much sense lmaoooo

And I have something like that, it’s the loop that keeps tripping me up because once the loop starts, I cant add onto the variable of time. Ive tried making it a function but the function seems as though it doesnt like to get called while a loop is running.

More of a #help-and-feedback:code-review suggestion rather than a #help-and-feedback:scripting-support suggestion but perhaps you should stay away from the “while wait do if condition break” type of while loop. It doesn’t make sense because the while do loop is meant to run when a given condition is true so just put the condition in its closure.

So: Dont do this

while wait(...) do
     if not (condition) then
          break
     end
end  

It doesn’t make sense. Just put the condition in the while do loop.

Do this!!

while (condition) do

     wait(...)
end
1 Like

That makes sense. I just added the if statement from prior into the wait statement:

while amnt >= os.time() do

Is there a way so that you can have a loop, then have a function change the value of a variable inside of a loop?

I kind of did this, but this doesnt work since I dont have an “Amount” variable prior, and if I add one it only uses that number and not any that I make amount out to be.

> local plr = game.Players.LocalPlayer
> 
> 
> local TimeScale = {
> 	SECOND = 0,
> 	MINUTE = 60,
> 	HOUR = 3600,
> 	DAY = 86400
> }
> 
> 
> 
> game.ReplicatedStorage.RED.OnClientEvent:Connect(function(amnt,Key)
> 	
> 	if Key == "VIPTimer" then
> 		if amnt >= os.time() then
> 			Amount = amnt
> 			
> 		end
> 		
> 	end
> 	
> end)
> 	
> 	while Amount >= os.time() do
> 			wait(1)
> 			print(Amount.." hehehehehe")
> 			local AmountTime =  Amount-os.time()
> 			
> 			local TimeInMinutes = math.ceil(AmountTime/60)
> 			local TimeInHours = math.floor(AmountTime/TimeScale.HOUR)
> 			
> 			
> 			script.Parent.Text = TimeInHours..":"..TimeInMinutes
> 			
> 			print (AmountTime)
> 			if AmountTime <= 0 then AmountTime = 0
> 				script.Parent.Text = "00:00"
> 				print(tostring(plr).."has "..AmountTime.." of VIP time left!")
> 				
> 			end
> 			
> 		end