Is there a better way to do this script?

Im new to scripting, and couldn’t figure out how I could make the script wait untill the “leaderstat” loaded in, so instead I gave it a number to use if it couldn’t find them

game.Players.PlayerAdded:Connect(function(player)
	local Waittime = 1
	local AddTime = 0
		if	player.leaderstats.Time.Value == 0 then
		AddTime = 0.00005
		else 
			AddTime = player.leaderstats.Time.Value * 0.0001 / 2
		end
		
	local Timer = 0
	
	while true do
		Timer = Timer + 1 
		if Timer >= 20 then
			Timer = 0
			AddTime = player.leaderstats.Time.Value * 0.0001 / 2
		end
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1
		print(player.Name, "'s added time", AddTime)
		wait(AddTime+Waittime)
	end
end)

This script basically increases a timer but with a delay depending on how high the timer is.
I’m making this post cause’ im curious and want to learn if

A: There is a better way to make this
or
B: How can I make it wait for the leaderstat to load in THEN continue the script?

1 Like

You can scrap all that waiting thing and just type this in

player:WaitForChild(“leaderstats”)

Ofc I don’t even think you need to wait for it because if the player loads then the leaderstats loads but give it a shot

1 Like

I have thought of this further, there is another script creates and loads a player’s leaderstat data, I think I need to make it wait for the data to finish loading then start it, alternatively, I should prolly add this code inside that other script

hey man,
i see you’re making a data saving script.

there’s this method called PreloadAsync i’m not too sure if it works on leaderstats but you can read about it here. it yields the script until all assets have loaded; that’s if you have multiple things that you want to be loaded before continuing your script.

https://create.roblox.com/docs/reference/engine/classes/ContentProvider#PreloadAsync

after it has loaded you can just do some sanitary checks to make sure that your content has loaded and if it has, you can resume your function within the scope of your checks by using i think it’s either coroutine.wrap or coroutine.resume. i’m not too sure tho.

example:

if loaded then --if leaderstats loaded
coroutine.wrap:Connect(function() --or coroutine.resume (idk)
--your code here
end)
end

or u could do task.spawn which fires ur function immediately

if loaded then --if leaderstats loaded
task.spawn(function() 
--your code here
end)
end

https://create.roblox.com/docs/reference/engine/libraries/coroutine#wrap
https://create.roblox.com/docs/reference/engine/libraries/task#spawn

1 Like

I think these options would’ve worked, but I instead went with just adding the code into the datasaving script which makes the leaderstate aswell

	local Ticker = 0
	local WaitTime = player.leaderstats.Bunage.Value * 0.0001
	while true do
		Ticker = Ticker + 1 
		if Ticker >= 40 then
			Ticker = 0
			WaitTime = player.leaderstats.Bunage.Value * 0.0001
		end
		player.leaderstats.Bunage.Value = player.leaderstats.Bunage.Value + 1
		wait(1+WaitTime)
	end
1 Like

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