I am trying to make the player move forward a few studs once they jump.
What have I tried?
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
localPlayer.Character.Humanoid.Changed:Connect(function()
if(localPlayer.Character.Humanoid.Jump == true) then
if localPlayer.Character then
local humanoid = localPlayer.Character:FindFirstChild("Humanoid")
if humanoid then
local Camera = workspace.CurrentCamera
while wait() do
local Direction = Vector3.new(Camera.CFrame.LookVector.X,0,Camera.CFrame.LookVector.Z)*1000
if not game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward") then
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
bodyForce.Name = "Forward"
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction
else
game.Players.LocalPlayer.Character.HumanoidRootPart:FindFirstChild("Forward").Force = Direction
end
end
end
end
end
end)
This didnt work however I also tried to do :Move to the player’s object and I tried to move the humanoid however that didn’t work?
Put this in a local script in StarterPlayerScripts. It should work unless I missunderstood your problem.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local STUDS_IN_SECOND = 10 -- a normal jump seems to take a little over half a second
local plr = Players.LocalPlayer
local jumping, landed = Enum.HumanoidStateType.Jumping, Enum.HumanoidStateType.Landed
local conns = {}
local isJumping = false
local function checkForLanding(_, newState)
if newState == landed then
conns.CheckForLanding:Disconnect()
conns.CheckForLanding = nil
isJumping = false
end
end
local function handleJump(hum, newState)
isJumping = true
conns.CheckForLanding = hum.StateChanged:Connect(checkForLanding)
local hrp = hum.Parent.HumanoidRootPart
local delta = 0
while isJumping do
hrp.CFrame *= CFrame.new(0, 0, delta*(-STUDS_IN_SECOND))
delta = RunService.Heartbeat:Wait()
end
end
local function onDeath()
for k, v in pairs(conns) do
v:Disconnect()
conns[k] = nil
end
isJumping = false
end
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
local stateChangedConn = hum.StateChanged:Connect(function(_, newState)
if newState == jumping then
handleJump(hum, newState)
end
end)
conns.Died = hum.Died:Connect(onDeath)
end)
Did you try using :MoveTo() on the humanoid object itself? :MoveTo(), when provided with a Vector3 of the goal position, will move a humanoid to the specified point. However, if a player inputs a movement while the :MoveTo() is running, that :MoveTo() call will be cancelled.
What you could do is disable the player’s movement options after they Jump or once they enter the .Landed state. This is how I disable movement, but there are other ways out there to do this. Just note that this needs to run client-sided; Running this server-sided won’t work. It basically overrides the WASD and Space keys with an empty function, but this function has more priority than the movement function built into Roblox’s default scripts.
local contextActionService = game:GetService("ContextActionService")
--To disable movement
contextActionService:BindActionAtPriority("DisableMovement", function()
return Enum.ContextActionResult.Sink
end, false, Enum.ContextActionPriority.Default.Value + 25, Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D, Enum.KeyCode.Space)
--To enable movement
contextActionService:UnbindAction("DisableMovement")