Running system!

I want to have a running “System”.

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?

3 Likes

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

3 Likes

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.

1 Like

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

1 Like

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.

1 Like

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

1 Like

Firing a remote every time is a perfect way to let someone spam the key and flood your remote events.

1 Like

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 :slight_smile: since throttling remotes can delay other legit remote fires ruining gameplay experience

1 Like

Ah, I didn’t know that, but like you said it can delay the other events.

3 Likes

I have done it locally and it works perfectly.

3 Likes

But I just realized that if I do it that way other players wont be able to see the animation

1 Like

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)

1 Like

Which I am, then how should I replicate it to other clients?

1 Like

It should do it automatically for you, humanoid animations by default replicate even when played locally

1 Like

Weird because it didn’t!!!
Maybe I did something wrong?

1 Like

Are you animating the Character to appears for everyone the animation or just for yourself like a FPS Hands Camera?

1 Like

Here’s the code, I don’t know why it doesn’t work

--// 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.

1 Like

is the script local or server-sided?

1 Like

oh yea where did you get the “Humanoid” and “HumanoidRootPart” is the variables not in the script or is it hidden?

1 Like

I get it from a module.
and the script that loads the animation is client side

1 Like