Creating charge energy system with remote events..?

Basically I have a stamina system but I need to change server side stamina when a player hold’s down the C key. Each player has a NumberValue called “Stamina” and another one called “MaxStamina”

In the local script normally I would do something like this…

local hold = false
userInputService.InputBegan:Connect(function(input, IsTyping)
	if IsTyping then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.C then
			hold = true
			while hold == true do
				wait(0.1)
				--Should I just spam the remote event here to update the server side?
			end
		end
	end
end

as you can see I use a while true do loop my only concern is spamming the server with remote :Fire()… Should I just do that or is there an easier way?

You could do that, yes. I don’t think it will cause any trouble.

Or just change it from the local script by accessing it. No other players needs to know the stamina from others, so that would make it client sided. However, it still would work.

Check for InputBegan and InputEnded. Example:

  • Player begins holding C: Fire remote event to server ONCE telling the server to constantly update the player’s charge every 0.01 second or so by a stable amount or a random amount ranging from let’s say 10-20 for example and update the client’s charge bar from the server.
  • Player stops holding C: Fire remote event to server and stop the charge.

This would reduce the number of remote event fires needed because the time it takes for communication between client-server is heavily reliant on ping. Players with high ping will have a significantly slower charge rate than those with low ping if you use the spam fire method.

1 Like

I agree with @vycVascense , unless you are trying to tie in a server-wide event when the stamina changes, i would just do it all on the client. If an exploiter wanted to cheat their movement speed, they wouldn’t even mess with the stamina system. Plus, requesting the current stamina from the server would show an obvious delay, which would worsen along with the connection.