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

How to launch an animation? I can't figure it out

I have a script and animation for punching, but I don't know how to make the player use the animation. I've been trying to figure it out so can someone help me.

Here's the code. It's a script in "ServerScriptService" The main part I'm trying to fix is lines 25 and 26

local uis = game:GetService("UserInputService")


game.Players.PlayerAdded:Connect(function(player)
	local animHold = Instance.new("StringValue")
	local plrgui = player:WaitForChild("PlayerGui")
	local setngs = plrgui:WaitForChild('Settings')
	local KeyReq = setngs:WaitForChild('Key')
	local frame = setngs:WaitForChild("Frame")
	local button = frame:WaitForChild("PunchBtn")
	local plr = player.Name
	local plrmodel = workspace:WaitForChild(plr)
	local char = plrmodel:WaitForChild("Character")
	local humanoid = char:WaitForChild("Humanoid")
	animHold.Name = 'Punch'
	local plr = workspace:WaitForChild(player.Name)
	local animate = plr:WaitForChild("Animate")
	animHold.Parent = animate
	local anim = Instance.new("Animation", animHold)
	anim.AnimationId = 'http://www.roblox.com/asset/?id=2889390323'
	if uis.KeyboardEnabled then
		uis.InputBegan:Connect(function(input, gameProcessedEvent)
			if input.KeyCode == Enum.KeyCode[KeyReq] then
				print('Punch')
				local animTrack = humanoid:LoadAnimation(anim)
				animTrack:Play()
			end
		end)
		uis.InputEnded:Connect(function(input, gameProcessedEvent)
			
		end)
		
	elseif uis.GamepadEnabled then
		uis.InputBegan:Connect(function(input, gameProcessedEvent)
			if input.UserInputType == Enum.UserInputType.Gamepad1 then
				if input.KeyCode == Enum.KeyCode.ButtonR2 then
					local animTrack = humanoid:LoadAnimation(anim)
					animTrack:Play()
					print("Punch")
		uis.InputEnded:Connect(function(input, gameProcessedEvent)
			
		end)
				end
			end
		end)
	end
end)

1 comment

1 answer

✓ Accepted answer

Server Script under ServerScriptService

local remote = Instance.new("RemoteEvent")
remote.Name = "PunchRemote"
remote.Parent = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)	
	player.CharacterAdded:Connect(function(char)		
		local humanoid = char:WaitForChild("Humanoid")
		local animate = char:WaitForChild("Animate")
		local animHold = Instance.new("StringValue")
		animHold.Name = "Punch"
		animHold.Parent = animate
		local anim = Instance.new("Animation")
		anim.Parent = animHold
anim.AnimationId = 'http://www.roblox.com/asset/?id=2889390323'
	end)
end)

remote.OnServerEvent:Connect(function(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	local anim = player.Character:WaitForChild("Animate"):WaitForChild("Punch"):WaitForChild("Animation")
	local animTrack = humanoid:LoadAnimation(anim)
	animTrack:Play()
	print("Punch")
end)

Local Script under StarterGui

local uis = game:GetService("UserInputService")
local remote = game.ReplicatedStorage:WaitForChild("PunchRemote")

local plrgui = script.Parent
local setngs = plrgui:WaitForChild("Settings")
local KeyReq = setngs:WaitForChild("Key")
local frame = setngs:WaitForChild("Frame")
local button = frame:WaitForChild("PunchBtn")

if uis.KeyboardEnabled then
	uis.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.KeyCode == Enum.KeyCode[KeyReq] then
			remote:FireServer()
		end
	end)
	
	uis.InputEnded:Connect(function(input, gameProcessedEvent)
		print("Ended")
	end)
elseif uis.GamepadEnabled then
	uis.InputBegan:Connect(function(input, gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Gamepad1 then
			if input.KeyCode == Enum.KeyCode.ButtonR2 then
				remote:FireServer()
			end
		end
	end)
	
	uis.InputEnded:Connect(function(input, gameProcessedEvent)
		print("Ended")
	end)
end

A. The player has an event called "CharacterAdded" which you can use when the player's character is respawned or created, this way you don't need to wait for the character using the workspace.

B. uis.InputBegan / InputEnded only work on the Client, so you need to use a Remote Event (created in the Server Script) to make them work.

C. You defined "plr" as a different local variable twice, which you can't do, since the second version will not overwrite the first.

D. [OPTIONAL], you may want to change (Enum.KeyCode[KeyReq]) to the name of the actual key you want pressed, since it seems you only have the "KeyReq" so as to set the keycode.

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