Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

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?

2 answers

✓ Accepted answer

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

33 comments

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

0

Also where do i put the events

0

No, put the remote events in the replicated storage :D

0

Here, this is for you, learn how to do remote events ok! :D

0

I got this error: Infinite yield possible on 'Workspace.kickoff127:WaitForChild("Torso")

0

and i dont see server script storage i only have serverscriptservice and server storage

0

Maybe your animation is for r6 and not for r15

0

Sorry, i meant serverscriptservice XD

0

oh ok its in r15

0

once i go down i cant go up then in a bit i go up and cant go back down

0

what line are you getting that error?

0

Priority is not a valid member of "Enum"

0

Script 'ServerScriptService.Script', Line 16

0

I changed it to Enum.AnimationPriority

0

Mine is way to complicated, just refer to the one who commented on your question

0

wait i think i have a different script than u or did u just change it

0

Also, i think he should be the answer.

0

yes, i changed it :D

0

Hey, can you send me the id of ur animation?

0

i got no error except Infinite yield possible on 'Workspace.kickoff127:WaitForChild("Torso") and it only worked once and not in the server

0

my animation id is 7110529161

0

Send me ur id of the animation. thx

0

i did

0

why

0

I think the error is that you put torso instead of humanoid

0

Can you put the animation on sale? thx

0

how do i fix it then

0

Did you put torso instead of humanoid in line 16 of script?

0

no its humaniod

0

Oops, i had a typo. Refresh the page and look at line 16 :D

0

same error

0

line 15 = plr.Character

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

This discussion is preserved as it was. New answers, comments, and votes are closed.