Log in to vote
0

Why Won't C to Crouch Work in Server, But it Does in Client?

Asked by 3 days ago
Edited 3 days ago

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.

0
If you set an animation to loop on the client, it won't loop on the server. Why not edit your animation to be looped by default and re-export it? radiant_Light203 1050 — 3d

2 answers

Log in to vote
0
Answered by 3 days ago
Edited 3 days ago

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 :)

0
im new to scripting and dont understand remote events to i just add them and name them uup and down the put them in the script kickoff127 17 — 3d
0
Also where do i put the events kickoff127 17 — 3d
0
No, put the remote events in the replicated storage :D XObbyCreatorX 38 — 3d
0
Here, this is for you, learn how to do remote events ok! :D XObbyCreatorX 38 — 3d
View all comments (29 more)
0
I got this error: Infinite yield possible on 'Workspace.kickoff127:WaitForChild("Torso") kickoff127 17 — 3d
0
and i dont see server script storage i only have serverscriptservice and server storage kickoff127 17 — 3d
0
Maybe your animation is for r6 and not for r15 XObbyCreatorX 38 — 3d
0
Sorry, i meant serverscriptservice XD XObbyCreatorX 38 — 3d
0
oh ok its in r15 kickoff127 17 — 3d
0
once i go down i cant go up then in a bit i go up and cant go back down kickoff127 17 — 3d
0
what line are you getting that error? XObbyCreatorX 38 — 3d
0
Priority is not a valid member of "Enum" kickoff127 17 — 3d
0
Script 'ServerScriptService.Script', Line 16 kickoff127 17 — 3d
0
I changed it to Enum.AnimationPriority XObbyCreatorX 38 — 3d
0
Mine is way to complicated, just refer to the one who commented on your question XObbyCreatorX 38 — 3d
0
wait i think i have a different script than u or did u just change it kickoff127 17 — 3d
0
Also, i think he should be the answer. XObbyCreatorX 38 — 3d
0
yes, i changed it :D XObbyCreatorX 38 — 3d
0
Hey, can you send me the id of ur animation? XObbyCreatorX 38 — 3d
0
i got no error except Infinite yield possible on 'Workspace.kickoff127:WaitForChild("Torso") and it only worked once and not in the server kickoff127 17 — 3d
0
my animation id is 7110529161 kickoff127 17 — 3d
0
Send me ur id of the animation. thx XObbyCreatorX 38 — 3d
0
i did kickoff127 17 — 3d
0
why kickoff127 17 — 3d
0
I think the error is that you put torso instead of humanoid XObbyCreatorX 38 — 3d
0
Can you put the animation on sale? thx XObbyCreatorX 38 — 3d
0
how do i fix it then kickoff127 17 — 3d
0
Did you put torso instead of humanoid in line 16 of script? XObbyCreatorX 38 — 3d
0
no its humaniod kickoff127 17 — 3d
0
Oops, i had a typo. Refresh the page and look at line 16 :D XObbyCreatorX 38 — 3d
0
same error kickoff127 17 — 3d
0
line 15 = plr.Character XObbyCreatorX 38 — 3d
Ad
Log in to vote
0
Answered by 3 days ago

UserInputService doesn’t work in a server script, only local scripts. This is because UserInputs are detected for each client.

Answer this question