I’m concerned about any future problems regarding creating multiple Heartbeat connections server-side, that handle players’ functions and attributes. For example, when one specific player presses the shift button locally, it fires a remote event that handles the player sprint. However, I’m using RunService to decrease the player’s stamina, meaning any player that starts sprinting will also create a connection, and so on. Another example being implemented in my game is the ‘AverageSpeed’ calculator function I’m using in my game. This function basically determines how fast the player is moving (studs/time): I’m also creating RunService connections for each character that joins/reloads in the game.
The main problem is, all those multiple connections would be really weighty to the game, as I’m creating connections for each player, which is bad. It may be possible to create a connection running locally, and then firing a remote event that updates the player stats. However, there are two visible problems for me:
- The player will send a lot of information to the server in a small quantity of times, multiple times, as I’m creating a loop running infinitely that fires the remote event.
- There’s a chance players’ exploit and ruins everything.
If you are wondering what the scripts are, take a look at them:
Sprint Script:
--// Useful part
local function animate(char, hum, run)
connection = RS.Heartbeat:Connect(function(dt)
acc += dt
if acc >= frametick then
acc = 0
local stam = char:GetAttribute("Stamina")
local moveDirection = hum.MoveDirection
local isMoving = moveDirection.Magnitude > 0
if run then
if not isMoving then
handleSprint(char, hum)
return
end
loseStam(char, hum, stam)
else
regenStam(char, stam, isMoving)
end
end
end)
end
local function changeStamina(char : Model, hum, run)
if connection and connection.Connected then
connection:Disconnect()
end
animate(char, hum, run)
end
handleSprint = function(char : Model, hum : Humanoid, run : boolean?, finishedStamina : boolean?)
local targetSpeed = run and 16 or 10 do
targetSpeed = finishedStamina and 6 or targetSpeed
end
changeStamina(char, hum, run)
sprint.animateSpeed(char, hum, targetSpeed)
end
sprintRemote.OnServerEvent:Connect(function(plr, run)
if not canSprint then return end
local char, hum = getComponentes(plr)
if not hum then return end
local moveDirection = hum.MoveDirection
local isMoving = moveDirection.Magnitude > 0
if not isMoving then return end
handleSprint(plr, hum, run)
end)
--// Rest of Script
Average Speed:
--// Module
local RS = game:GetService("RunService")
return function(char : Model)
local hum = char:WaitForChild("Humanoid")
local HRP = char:WaitForChild("HumanoidRootPart") :: BasePart
local previousPosition = HRP.Position
RS.Heartbeat:Connect(function(DT)
local currentPosition = HRP.Position
local delta = currentPosition - previousPosition
delta = Vector3.new(delta.X, 0, delta.Z)
local distance = delta.Magnitude
char:SetAttribute("AverageSpeed", distance/DT)
previousPosition = currentPosition
end)
end
--// ServerScript
--// Useful part
local function initializeCharacter(char : Model)
local rawWalkSpeed do
rawWalkSpeed = Instance.new("IntValue")
rawWalkSpeed.Name = "RawWalkSpeed"
rawWalkSpeed.Value = 10
rawWalkSpeed.Parent = char
end
char:SetAttribute("Stamina", 100)
char:SetAttribute("AverageSpeed", 0)
averageSpeed(char) -- AverageSpeed is the function inside of a Module I just sent
end
plrs.PlayerAdded:Connect(function()
plr.CharacterAdded:Connect(initializeCharacter)
end)
As you can see, both scripts create a lot of connections for each player. So, my question is, how can I do that without damaging the game’s efficiency?