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

Press B *Loads Animation* Wont work? Using RemoveEvents!

Hello can anyone help as to why this code wont work. It is meant to play an animation when I press B. Thanks :D

Main Script:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character

game.ReplicatedStorage.BBQPressBToUse.OnServerEvent:Connect(function(plr)
	UIS.InputBegan:connect(function(input) 
		if input.KeyCode == Enum.KeyCode.B then
			Character.Humanoid.Walkspeed = 16
			local Anim = Instance.new("Animation")
			anim.AnimationId = "rbxassetid://2305757864"
			local PlayAnim = Character.RightArm:LoadAnimation(Anim)
			PlayAnim:Play()
		end
	end)


-- Scripting Helpers: Not completed down below :D	
UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.B then
		Character.Walkspeed = 16 
	end
end)
	

LocalScript (For RemoteEvent)

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.B then
		game.ReplicatedStorage.BBQPressBToUse:FireServer()
	end
end)

Thanks ;D

1 comment

0

It's not character.walkspeed it's character.Humanoid.Walkspeed. Please post a picture of your error log.

1 answer

The problem you have is line 11, where you are loading the animation via the character's right arm instead of the humanoid. Here is your server script redone with the changes :) Your Local Script is fine. ALSO NEVER USE USERINPUTSERVICE on the server, that service is client-sided only. Never use the local player and character on the server either, use the player parameter given when firing the remote.

--Server Script

--NO USERINPUTSERVICE ON THE SERVER
--No LocalPlayer on the server

game.ReplicatedStorage.BBQPressBToUse.OnServerEvent:Connect(function(plr)
	--Don't need the userinput stuff here.
	    local char = player.Character or player.CharacterAdded:Wait() -- Char Variable
	    char.Parent = workspace -- To avoid bugs.
            char.Humanoid.Walkspeed = 16
            local Anim = Instance.new("Animation")
            anim.AnimationId = "rbxassetid://2305757864"
            local PlayAnim = char.Humanoid:LoadAnimation(Anim) --Load it on the humanoid.
	    PlayAnim.Priority = Enum.AnimationPriority.Movement --Setting the animation priority.
            PlayAnim:Play()
	    --Don't need the second end because we don't need the user input twice, especially not on the server.
    end)

--Never Use UserInputService in a server script.

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