I have a circular health meter that positions itself under the target player or NPC using RenderStepped
(for players) and Heartbeat
(for NPCs.) Now, it works just fine for the players. It's smooth, looks nice, and is not jittery. But for the NPCs using Heartbeat
, it can't keep up with the movement of the NPCs with a WalkSpeed
of 8. It trails behind, visibly lagging.
So, is there a smoother alternative to Heartbeat
for server-side scripts? If not, what scripting magic can I do to make this work?
while
loops, Stepped
, and RenderStepped
(which isn't even supposed to be used server-side) are all even more jittery.
I am repositioning parts, not GUIs.
There is no way for you to get a server-side script to do something at 0.016 seconds without a local script. So when you're a coder you need to think of solutions for your problem. What is one solution? Well, use remote events of course! You can connect Local Scripts to Server-Sided ones, you don't have to move the parts with a local script so you can get reliable movement and its FE compatible! A win-win-win!
Insert the remote event into the server script for this example. I have a local and server script. I need the local script to make a part with the player's name. How do we do that?
local part = Instance.new("Part",workspace) --Method 1; ew. If there is FE enable then it'll only be client-sided, avoid unless you're actually using local parts! part.Name = game:GetService("Players").LocalPlayer.Name --it's a pain to get the name(rhymes lol) --Method 2 workspace.ServerScript.RemoteEvent:FireServer() --FireServer is an event that only fires the server scripts that uses OnServerEvent.
script.RemoteEvent.OnServerEvent(function(player) --The player who fired the server will always be the first argument automatically. local part = Instance.new("Part",workspace) part.Name = player.Name end) --Clean, FE compatible and uses server script.
The reason why HeartBeat
is local script only is because it fires every time a frame runs on the player's computer, which is 60 frames per second, but since it depends on the client's computer speed it must be local. That's the reason why it can only be used in Local Scripts.
What I did here was I used HeartBeat
inside of the local script that will fire the server every time. After it does that then the server will continuously move a model upwards.
game:GetService("RunService").HeartBeat:connect(function() --Every 1/60th of a second(If the computer lags then it'll be around 45 frames per second.) workspace.Script.RemoteEvent:FireServer() end)
script.RemoteEvent.OnServerEvent:connect(function() workspace.Model:SetPrimaryPartCFrame(workspace.Model.PrimaryPart.CFrame*CFrame.new(0,1,0) --Move 1 stud up in World Space. end)
What I would do is have the server script move and control all of the NPCs hearts at once so that all the players are controlling the heart's movements. You can also make the hearts local and turn on FE because all the players will be seeing the same thing. Just work off what I did and try to make it work, but I'd make one single server script control all of the NPCs and will move the heart behind it while the function is firing.
Hope it helps!