I was trying to make a game where if you press "C", you will crouch. I have the script, and here it is.(I also put an animation into the local script and put in an animation ID I made)
local UserInputService = game:GetService("UserInputService") local Player = game.Players.LocalPlayer local Character = Player.Character or Player.CharacterAdded:wait() local Humanoid = Character:WaitForChild("Humanoid") local Animation = script:WaitForChild("Animation") local Track = Humanoid:LoadAnimation(Animation) Track.Looped = true Track.Priority = Enum.AnimationPriority.Action local function CheckKeyDown(Key) if Key.KeyCode == Enum.KeyCode.C then Track:Play() Humanoid.WalkSpeed = 8 end end local function CheckKeyUp(Key) if Key.KeyCode == Enum.KeyCode.C then Track:Stop() Humanoid.WalkSpeed = 16 end end UserInputService.InputBegan:Connect(CheckKeyDown) UserInputService.InputEnded:Connect(CheckKeyUp)
Is there something I can add to make it work in the server? Thanks.
Try adding a fireserver function.
Like this :
local UserInputService = game:GetService("UserInputService") local Player = game.Players.LocalPlayer local Character = Player.Character or Player.CharacterAdded:wait() local Humanoid = Character:WaitForChild("Humanoid") local CrouchStart = game.ReplicatedStorage.("your remote event") local CrouchEnd = game.ReplicatedStorage.("your other remote event") local function CheckKeyDown(Key) if Key.KeyCode == Enum.KeyCode.C then local Animation = script:WaitForChild("Animation") CrouchStart:FireServer(Animation) end end local function CheckKeyUp(Key) if Key.KeyCode == Enum.KeyCode.C then local Animation = script:WaitForChild("Animation") CrouchEnd:FireServer(Animation) end end UserInputService.InputBegan:Connect(CheckKeyDown) UserInputService.InputEnded:Connect(CheckKeyUp)
Then add a script in the serverscriptstorage :
local CrouchStart = game.ReplicatedStorage.("your remote event") local CrouchEnd = game.ReplicatedStorage.("your other remote event") CrouchStart.OnServerEvent:Connect(function(plr, Animation) local char = plr.Character or plr.CharacterAdded:Wait() local hum = char:WaitForChild("Humanoid") local Track = hum:LoadAnimation(Animation) Track.Priority = Enum.AnimationPriority.Action Track.Looped = true Track:Play() hum.WalkSpeed =8 end) CrouchEnd.OnServerEvent:Connect(function(plr, Animation) local char = plr.Character or plr.CharacterAdded:Wait() local hum = char:WaitForChild("Humanoid") local Track = hum:LoadAnimation(Animation) Track.Looped = false Track:Stop() hum.WalkSpeed = 16 end)
Comment if there is an error :)
UserInputService doesn’t work in a server script, only local scripts. This is because UserInputs are detected for each client.