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

The animation is playing but the character is not changing?

I'm trying to make it so when you click a button your character does an animation and then his character is changed, when he does it again he goes back to his normal character.

Server:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HoodUp= ReplicatedStorage.HoodUp
local HoodDown= ReplicatedStorage.HoodDown
local hoodupchar = ReplicatedStorage.HoodUpChar
local hoodupcharclone = hoodupchar:Clone()


HoodUp.OnServerEvent:Connect(function(Player, Name)	
	local UserId = Players:GetUserIdFromNameAsync(Name)
	local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(UserId)

	Player.Character = hoodupcharclone
end)

HoodDown.OnServerEvent:Connect(function(Player, Name)	
	local UserId = Players:GetUserIdFromNameAsync(Name)
	local HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(UserId)

	Player:LoadCharacterWithHumanoidDescription(HumanoidDescription)
end)

Client

local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
playing = false
character = false

script.Parent.MouseButton1Click:connect(function()
	if playing == false then
		if character == false then
			emote:Play()
			playing = true
			wait(emote.Length - 0.1)
			local ReplicatedStorage = game:GetService("ReplicatedStorage")
			local ChangeCharacter = ReplicatedStorage.HoodUp

			script.Parent.MouseButton1Click:Connect(function()
				ChangeCharacter:FireServer(player)
				character = true
			end)
			wait(0.1)
			playing = false
		end
	end
	
	if playing == false then
		if character == true then
			emote:Play()
			playing = true
			wait(emote.Length - 0.1)
			local ReplicatedStorage = game:GetService("ReplicatedStorage")
			local ChangeCharacter = ReplicatedStorage.HoodDown

			script.Parent.MouseButton1Click:Connect(function()
				ChangeCharacter:FireServer(player)
			end)
			wait(0.1)
			playing = false
		end
	end
end)

1 answer

✓ Accepted answer

You know that you can just load the animation on the client because client animation replicates to the server so everyone can see it, right? Here is the client code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = -- directory to the animation, if doesn't work use :WaitForChild to select it
local track = animator:LoadAnimation(animation)

Track:Play()
-- if you wanna put it in a event, just put that statement into the event, not the variables.

Also if you wanna do it server-sided here is it:

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local character = player.Character 
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = -- directory to animation
local Track = animator:LoadAnimation(animation)

Track:Play()
end)
end)

Why we don't use player.CharacterAdded:Wait() because the characteradded event is when the character is added, that means it already exists so we don't have to use the :Wait() like the client code.

Hope this helped :D

3 comments

0

thanks i'm going to try to make it work

0

wait how would i make it so he can toggle on a character and toggle off a character?

0

I edited my post so you can see the client sided script

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