So i’m working on a space game. and i want to be able to have a shield that absorbs a percentage of damage damage, loses points from taking damage. and recharge at a certain rate.
I’ve got the first two figured out, but what i’m stuck on is how often i should do the serverside calculations for recharging shields and warp, cooling reactors, etc.
Do you how exactly the shields, warp, and cooling reactors update?
If you know the exact rate as a function of time, the most efficient way would be to store the last time of update, and then dynamically calculate it when you need something done. This makes the load not scale as bad with the number of players and also lets you do it at whatever granularity you want.
That being said, it does add a bit of complexity to your game. There is a case to be made here for just keeping the update rate to some multiple of the server heartbeat rate.
My primary concern is trying to run too much code and things start to slow down for everyone.
i know how exactly how, it’ll be Value = Value + Effect*Delta
if i read your thread correctly, you’re suggesting i adjust the tick speed with player count, having a longer tick interval when more players are on?
That isn’t what my first suggestion was. However, in your suggestion, you shouldn’t need to adjust the tick speed manually, since the time in between heartbeats automatically increases if the server is lagging. The best solution if you want to do this would probably be to update at a constant multiple of the heartbeat. For example, you can update every 5 heartbeats. This should keep the update responsive enough to feel good, but also not too laggy.
mk, and what would be the best method of getting that tick out?
Should i just have a script in each player counting heartbeats? or should i have the main server script be firing a bindable event every 5 heartbeats?
I would put all of the updates in one script, like this:
local RunService = game:GetService("RunService")
local counter = 0
RunService.Heartbeat:Connect(function()
if (counter < 4) then
counter = counter + 1
else
counter = 0
-- do updates for every player --
end
end)