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

I don't get Remote Events... Help me script this animation to run!?

There is Absolutely NO videos on yt on how to make an animation run with a remote event... Right now, I press "c" and nothing happens. I've been trying and trying to make this work but nothing does. Pls help

This is my localScript

--LocalPlayer's Camera
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character

--Crouch Animation
local mouse = player:GetMouse()
local Humanoid = player.Character:FindFirstChild('Humanoid')
local CrouchEvent = game:GetService("ReplicatedStorage"):WaitForChild("CrouchEvent")
local AlreadyCrouched = false

CrouchEvent.OnClientEvent:Connect(function()
	mouse.KeyDown:Connect(function(Key) 
   		if Key == "c" and AlreadyCrouched == false then
            	Humanoid.CameraOffset = Vector3.new(0, -1.2, -1.5)
				CrouchEvent:FireServer()
            	AlreadyCrouched = true
            	print("Crouched")
    	elseif Key == "c" and AlreadyCrouched == true then
            	Humanoid.CameraOffset = Vector3.new(0, 0.2, -1.5)
            	AlreadyCrouched = false
            	print("unCrouched")
    	end
	end)
end)

This is my Script

local CrouchEvent = game:GetService("ReplicatedStorage"):WaitForChild("CrouchEvent")

CrouchEvent.OnServerEvent:Connect(function()
	local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=4835659821"
	CrouchEvent:FireClient()
end)

1 answer

✓ Accepted answer

You don't need remote events for this. Playing animations on your local character will replicate to other players. So just load the animation locally and play it.

Here's what your code should look like

--LocalPlayer's Camera
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character

--Crouch Animation
local mouse = player:GetMouse()
local Humanoid = player.Character:FindFirstChild('Humanoid')
local AlreadyCrouched = false

local Animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=4835659821"

local AnimationTrack = Humanoid:LoadAnimation(Animation)

mouse.KeyDown:Connect(function(Key) 
    if Key == "c" and AlreadyCrouched == false then
            Humanoid.CameraOffset = Vector3.new(0, -1.2, -1.5)
            AlreadyCrouched = true
            AnimationTrack:Play()
            print("Crouched")
    elseif Key == "c" and AlreadyCrouched == true then
            Humanoid.CameraOffset = Vector3.new(0, 0.2, -1.5)
            AlreadyCrouched = false
            AnimationTrack:Stop()
            print("unCrouched")
    end
end)

Keep in mind that since the player's character is replaced everytime they die and respawn, and thus a new humanoid is created, you have to use the new humanoid to load the animation. You can simply just place this local script in game.StarterPlayer.StarterCharacterScripts


If you want to learn more about remote events, there are resources online to teach you how they work. Here is the wiki link on remote events https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Here is a wiki link on the client-server structure which you have to understand to understand remotes: https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

2 comments

0

Please use UserInputService's .InputBegan event instead. Mouse.KeyDown is a deprecated RBXScriptSignal

0

This code is what I had before except when I go in game (with 2 accounts) and check. It doesn't show the animation.

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