Hello developers,
I am currently making a game where you basically control a block in the 3D workspace. It should move 5 studs in all directions, though I am not entirely sure I would set this up securely. I know how to handle all input, I just need to know how can allow the client to for example press WASD and have it move on the server so it replicates correctly.
Code I have currently (works decent but has lots of bugs and is not ideal for high ping players):
local UserInputService = game:GetService("UserInputService")
local RequestMovementNetwork = EngineClient.CreateNetworkSignal("RequestMovement")
-- // Variables
-- // Functions
local function RequestMovementLocally(movementKeyCode: Enum.KeyCode, movementInputType: Enum.InputType)
RequestMovementNetwork:Fire({ -- Fire the movement network signal to notify the server of a player input
movementKeyCode, -- Send over the keycode to evaluate which way to move and if the keycode is valid for player movement
movementInputType -- Send over the input type to see if the player is moving their character with a different device
})
end
function Package.RequestMovement(movementKeyCode: Enum.KeyCode, movementInputType: Enum.InputType) -- Expose the movement request API to allow modded movement
RequestMovementLocally(
movementKeyCode,
movementInputType
)
end
local function CheckForKeyDownMovement(inputObject, gameProcessed)
if gameProcessed then -- If the keycode is being used by the client itself end the function to prevent issues
return
end
while UserInputService:IsKeyDown(inputObject.KeyCode) do -- Request movement while the input keycode is down
Package.RequestMovement(inputObject.KeyCode, inputObject.UserInputType)
task.wait(1)
end
end
I used a while loop so I could allow players to just hold down for example S and keep going down until they hit the map boundary.