Custom jump physics not working in multiplayer

Hi everyone,

I’ve been working on a 2D platformer, and a part of that custom movement is the jump. The jump is supposed to go as follows:

While mid-air, the player gets a slight boost upwards while holding jump. This makes it so holding jump gives you slightly floatier physics. This effect is ramped up (higher boost) when running, so your jumps are even floatier when running.

I’ve been recently testing this with my friend. For some reason, only I could have the boosted jump, but for some reason my friend couldn’t. This was also the case when they joined first, and in other scenarios. (For reference, we used Team Test in Studio).

My code can be found below. If anyone has any ideas of how to fix this, please help!

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false
local running = false
local leftValue, rightValue = 0, 0

local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		leftValue = 1
	elseif inputState == Enum.UserInputState.End then
		leftValue = 0
	end
end

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		rightValue = 1
	elseif inputState == Enum.UserInputState.End then
		rightValue = 0
	end
end

local function onJump(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		jumping = true
	elseif inputState == Enum.UserInputState.End then
		jumping = false
	end
end

local function onUpdate()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		
		if player.Character == nil then
			return
		end
		
		if player.Character:FindFirstChild("HumanoidRootPart") == nil then
			return
		end
		
		player.Character.HumanoidRootPart.AssemblyLinearVelocity -= Vector3.new(0, 0.5, 0)
		if jumping then
			player.Character.Humanoid.Jump = true
			local linVelocity = player.Character.HumanoidRootPart.AssemblyLinearVelocity
			player.Character.HumanoidRootPart.AssemblyLinearVelocity += Vector3.new(0, (player.Character.Humanoid.WalkSpeed - 12)/10, 0)
			
			if linVelocity.Y >= 0 then
				player.Character.HumanoidRootPart.AssemblyLinearVelocity += Vector3.new(0, 1.35, 0)
			end
		end
		
		local speed = player.Character.Humanoid.WalkSpeed
		if running and player.Character.Humanoid.MoveDirection.Magnitude > 0 then
			if speed < 24 then
				player.Character.Humanoid.WalkSpeed += 0.075
			end
			
			if speed > 24 then
				player.Character.Humanoid.WalkSpeed = 24
			end
		else
			if speed > 16 then
				player.Character.Humanoid.WalkSpeed -= 0.1
				
				if player.Character.Humanoid.MoveDirection.Magnitude == 0 then
					player.Character.Humanoid.WalkSpeed -= 0.05
				end
			elseif speed < 16 then
				player.Character.Humanoid.WalkSpeed = 16
			end
		end
		
		local moveDirection = rightValue - leftValue
		player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
	end
end

local function onRun(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		running = true
	elseif inputState == Enum.UserInputState.End then
		running = false
	end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "a", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "d", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Jump", onJump, true, "w", Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA, Enum.KeyCode.ButtonB)
ContextActionService:BindAction("Run", onRun, true, Enum.KeyCode.LeftShift, Enum.KeyCode.RightShift, Enum.KeyCode.ButtonX, Enum.KeyCode.ButtonY)
2 Likes

For reference, the jump worked perfectly fine when my friend tested it in singleplayer, normal Testing in Studio.

That’s because it’s a local script… use a server/normal script

Then how am I supposed to get the input or the player? The script which roblox itself uses is a local script, too.

To resolve this issue and ensure all players can experience the custom jump mechanics as intended, consider this

Server Script (e.g., in a Script in ServerScriptService)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Assuming these RemoteEvents have been created in ReplicatedStorage
local JumpEvent = ReplicatedStorage:WaitForChild("JumpEvent")
local RunEvent = ReplicatedStorage:WaitForChild("RunEvent")

local function onJump(player, isJumping)
    -- Handle jump logic here, e.g., modifying player's jump height or applying forces
end

local function onRun(player, isRunning)
    -- Handle run logic here, e.g., adjusting player speed or jump behavior when running
end

JumpEvent.OnServerEvent:Connect(onJump)
RunEvent.OnServerEvent:Connect(onRun)

Local Script (e.g., in a LocalScript under StarterPlayerScripts)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")

local JumpEvent = ReplicatedStorage:WaitForChild("JumpEvent")
local RunEvent = ReplicatedStorage:WaitForChild("RunEvent")

local function onJump(actionName, inputState, inputObject)
    local isJumping = (inputState == Enum.UserInputState.Begin)
    JumpEvent:FireServer(isJumping)
end

local function onRun(actionName, inputState, inputObject)
    local isRunning = (inputState == Enum.UserInputState.Begin)
    RunEvent:FireServer(isRunning)
end

-- Bind actions
ContextActionService:BindAction("Jump", onJump, false, Enum.KeyCode.Space)
ContextActionService:BindAction("Run", onRun, false, Enum.KeyCode.LeftShift, Enum.KeyCode.RightShift)

This outline demonstrates the initial steps toward implementing a system where jump and run actions are communicated from the client to the server, which then applies the game logic in a way that’s consistent across all clients. You’ll need to fill in the specific logic for handling jumps and runs according to your game though

2 Likes

Okay, i’ll let you know how that goes!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.