Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I bind "W" to jump using scripts?

Asked by
LeeRob 2
4 years ago

Hi, I'm extremely new to roblox coding and I want to make a traditional fighting game. The first thing I'm trying to do is make it so W jumps and S crouches, but I'm not even sure where to start. I'm sure it's possible because in the "Infinite Runner" preset game you use W to jump. I don't have any code to show because I haven't been able to find anything related to my problem. Thanks!

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It's not very complicated. You just need to understand how to get a player's keypress and need to understand remote events.

Make sure this is a local script inside of startergui:

--LOCAL SCRIPT
--PUT THIS SCRIPT INSIDE OF STARTERGUI

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local jumpEvent = game:GetService("ReplicatedStorage"):WaitForChild("jumpEvent") --Waits for the remoteEvent to be created by the server

mouse.KeyDown:Connect(function(key)
    if string.lower(key) == "w" then
        jumpEvent:FireServer() --send a message to the server to tell the player to jump
    end
end)

Make sure this is a server script:

--SERVER SCRIPT

local jumpEvent = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")) -- create the remoteEvent
jumpEvent.Name = "jumpEvent"

jumpEvent.OnServerEvent:Connect(function(player) -- receive the message from the local script
    print("Jumped")
    player.Character.Humanoid.Jump = true
end)

The reason we need two scripts is because server scripts cannot get a player keypress and local scripts cannot make the player jump. So we use the remote events to let them communicate to each other.

0
Thank you so much! LeeRob 2 — 4y
Ad

Answer this question