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

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

Asked by 3 years ago
Edited 3 years 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 1166 — 3y

2 answers

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

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

Answer this question