I currently fire a remote for every keypress like this:
--// When an input begins or ends
UserInputService.InputBegan:Connect(function(input, gameProcessed)
--// If the input is a keyboard input
if not gameProcessed and input.UserInputType == Enum.UserInputType.Keyboard then
--// Random check
InputEnded = false
SendInputEvent:FireServer(input.KeyCode, InputEnded)
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if not gameProcessed and input.UserInputType == Enum.UserInputType.Keyboard then
InputEnded = true
SendInputEvent:FireServer(input.KeyCode, InputEnded)
end
end)
Edit 1:
Is there anyway I can make it more efficient
Edit 2:
Is doing all the code in the server worth the hustle.
And if not is changing my animation and walkspeed on the client is the correct way to do it?
By running I assume you’re talking about movement players running, I don’t recommand using remotes for this because there going to be a obvious delay from when the player presses input key to them actually running; I would simply do it locally without remotes
If your worried about security then you can make a server script that recorrects the player position if they move to fast
Instead of a remote being fired on every keypress you could add a debounce which prevents the function from executing too often & you could change the running system to work on a toggle, one keypress fires the server which enables running and a successive keypress of the same key will also fire the server but this time disabling the running.
That still have the issue of remote latency, making it very delayed from running to non running, would still recommand not using remotes at all for the best gameplay experience
It would vary depending on the ping of the end-user but for a toggling system (this shouldn’t be too bad for most). You’d get a slight initial delay when enabling the running toggle & subsequently another slight delay when disabling the same toggle (in between the 2 actions the game will run smoothly), this is better than experiencing a delay for each frame the run button is held for.
I agree its better then experiencing delay for each frame, which is why I suggest to just do it entirely local; although if your fine with the inital delay then I could see that being a valid reason to use your method, personally for me I would mind such a delay; Although it does depend on user ping which in my experience I usually average around 80 ping so the delay for me would be longer then average players
Remotes actually have special throttling behavior if 2 many remotes are fired, this is why hackers can’t lag servers just by simply firing remotes, since the remote calls will throttle back for that player (typically when hackers do lag servers by firing remotes that due to the coder fault), Although still good practice to fire less remotes whenever possible since throttling remotes can delay other legit remote fires ruining gameplay experience
Assuming your using an actual normal player character, Animations that are played locally will automatically replicate to other clients (unless your using a local animator)
--// Animation
local AnimateScript = Humanoid:FindFirstChild("Animator")
local Animation = Character:WaitForChild("Animate").run.RunAnim
warn(Animation)
local RunAnimation = AnimateScript:LoadAnimation(Animation)
local function shiftSprint(actionName, inputState, inputObj)
local Speed = (HumanoidRootPart.Velocity).magnitude
if inputState == Enum.UserInputState.Begin and Speed > 1 then
Running = true
Humanoid.WalkSpeed = 25
RunAnimation:Play()
task.spawn(function()
while Running do
RenderStepped:Wait()
local Speed = (HumanoidRootPart.Velocity).magnitude
if Speed < 1 then
Running = false
Humanoid.WalkSpeed = 9
RunAnimation:Stop()
break
end
end
end)
elseif inputState == Enum.UserInputState.End and Running then
Running = false
Humanoid.WalkSpeed = 9
RunAnimation:Stop()
elseif inputState == Enum.UserInputState.Begin and Speed < 1 then
task.spawn(function()
while true do
local Speed = (HumanoidRootPart.Velocity).magnitude
RenderStepped:Wait()
if Speed > 1 then
Running = true
Humanoid.WalkSpeed = 25
RunAnimation:Play()
break
end
end
end)
end
end
ContextActionService:BindAction("ShiftSprint", shiftSprint, false, Enum.KeyCode.LeftShift)
It doesn’t replicate to the server when I play the running animation.