How to capture/update a changing variable in RunService?

I have a working horse that moves at a Humanoid.WalkSpeed of 25.
When I press LShift, the Humanoid.WalkSpeed should update to 50 (it does in the test game server, as you can see in the WalkSpeed Property Output picture).

My problem is that the Server Script code uses RunService.Heartbeat and the Humanoid.WalkSpeed does not update from 25. So the horse never moves any faster.

Scripts

Local Script
local ContextActionService = game:GetService("ContextActionService")

-- If player presses left shift key, this function is called by ContextActionService
local function handleAction(actionName, inputState, inputObject)
	if actionName == "ShiftTest" then
		if inputState == Enum.UserInputState.Begin then
			print("Pressing Shift")
			horse.Humanoid.WalkSpeed = 50
			game:GetService("ReplicatedStorage").IsRunning:InvokeServer()
		else
			print("Stopped pressing shift")
			horse.Humanoid.WalkSpeed = 25	
			game:GetService("ReplicatedStorage").IsWalking:InvokeServer()
		end
	end
end

-- Binding LeftShift Key to handleAction function
ContextActionService:BindAction("ShiftTest", handleAction, true, Enum.KeyCode.LeftShift)
Server Script
game:GetService("ReplicatedStorage").IsRunning.OnServerInvoke = function (player) Humanoid.WalkSpeed = 50 end
game:GetService("ReplicatedStorage").IsWalking.OnServerInvoke = function (player) Humanoid.WalkSpeed = 25 end

function Animate(Time)
       ...
    -- Motion
	if Seat.Throttle ~= -1 then
		Velocity.velocity = (-Torso.CFrame.lookVector * Seat.Throttle * Humanoid.WalkSpeed*-1)
		AngularVelocity.AngularVelocity = Vector3.new(0, (1.7 * -Seat.Steer), 0)--steer speed
		print(Humanoid.WalkSpeed)	
	else	
       ...
end

RunService.Heartbeat:connect(Animate)
Output: 25 (repeated)

Outputs

Console Log Output

WalkSpeed stays at 25

WalkSpeed Property Output

No Shift:
Walkspeed25
With Shift:
Walkspeed50

WalkSpeed updates to 50 when Shift is pressed, but horse does not move faster. RunService just captures speed at 25 and never updates

What I Need Help With: RunService in Server Script needs to capture the Humanoid.WalkSpeed changes. Currently trying Humanoid.Changed:connect functions with no avail. I’m not sure how to tell the script that there was a change in speed. Thanks in advance

A BindableEvent may help. Each time you change WalkSpeed, you could then manually fire your event.

Does the client have ownership of the horse? Try setting Horse.HumanoidRootPart:SetNetworkOwner(player) to make sure your changes replicate to the server.

Edit: You can check the WalkSpeed on the server while testing by navigating to the Test panel at the top of Studio, clicking the Current: Client button to change to server, and find the Humanoid in workspace.

turns out i had a piece of code in the wrong spot in my script, now it works :sob:

i thought this would be the case, but i ended up solving my issue thanks so much though!

1 Like