I need to make my scripts run faster

I have some scripts that run in the background to give the player points every so often. Unfortunately, there is a delay of about a minute, so I would like to fix this. Here is the script:

local ServerStorage = game:GetService("ServerStorage")
local addPoints = require(ServerStorage:WaitForChild("AddPoints"))

while true do
	wait(1800)
	for currentValue = 1, 3 do
		for i, Plr in pairs(game.Players:GetPlayers()) do
			addPoints.addPoints(Plr)
		end
	end
end

If you have any ideas on how to make this faster, please say.

Sorry if this is the wrong section, I felt that this was the best place to put it.

1 Like

wait(1800) is going to yield the script’s execution for 1,800 seconds, which is 30 minutes.

1 Like

I know that, but its taking more than 1800 seconds, which is what I’m trying to fix.

Use task.wait() instead as it’s more accurate.

local function Loop()
	delay(1800, Loop)
	for i, player in ipairs(game.Players:GetPlayers()) do
		addPoints.addPoints(player, 3)
	end
end
Loop()

or

local runService = game:GetService("RunService")
local timer = 0
runService.Heartbeat:Connect(function(deltaTime))
	timer += deltaTime
	if timer < 1800 then return end
	timer -= 1800
	for i, player in ipairs(game.Players:GetPlayers()) do
		addPoints.addPoints(player, 3)
	end
end)

Can you please explain what those do?

Instead of wait(1800) just use task.wait(1800).

the only problem with task.wait(1800)

is that we dont know how long it takes for

for currentValue = 1, 3 do
	for i, Plr in pairs(game.Players:GetPlayers()) do
		addPoints.addPoints(Plr)
	end
end

to complete

so the total time will be 1800 + how long it takes to do the above code

So it still might have a 1 minutes extra delay even if task.wait() is uses

local function Loop()
	-- its also possible to use task.delay here instead of delay if you want
	delay(1800, Loop) -- tell lua to run the loop function after 1800 seconds

	-- this part of the function has no effect on the above delay so even if this part takes a long time to complete it will not effect the above delay
	for i, player in ipairs(game.Players:GetPlayers()) do
		addPoints.addPoints(player, 3)
	end
end
-- run the loop function
Loop()
local runService = game:GetService("RunService")
local timer = 0
-- the heartbeat event fires every frame and the deltaTime value tells us how long it took from the previous frame task.wait() also uses the Heartbeat event so it has the same accuracy
runService.Heartbeat:Connect(function(deltaTime))
	-- increment timer by the amount of time that past since the event was previously called
	timer += deltaTime
	-- if the timer is still below 1800 seconds then return and do nothing
	if timer < 1800 then return end
	-- if the timer is more then or equal to 1800 we subtract timer by 1800
	-- the reason we don't set time simple back to 0 is because timer might have a value of 1800.31792
	-- and so that we dont lose the 0.31792 we subtract by 1800 so that we have the highest accuracy
	timer -= 1800
	-- now we can do what we like this will not effect when the next heartbeat event gets fired so this part of the code can be as slow as you want
	for i, player in ipairs(game.Players:GetPlayers()) do
		addPoints.addPoints(player, 3)
	end
end)

you can read more about task here

2 Likes