How do I change character animations upon touching a part?

  1. What do you want to achieve? Keep it simple and clear!

I’m attempting to make a part that changes the player’s animations when touched. Once it’s touched, it reaches into the player’s Animation script and alters the IDs.

  1. What is the issue? Include screenshots / videos if possible!

Although it works, I noticed that the animations go back to the previous ones upon death.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried a few solutions, my first draft is what I just described to you. My second draft involved using a script in ServerScriptService, but it couldn’t be enabled. I looked all over the developer hub but to no avail, any help would be appreciated. Here’s my script:

function onTouched(Obj)
	local h = Obj.Parent:FindFirstChild("Humanoid")
	if h then
		Obj.Parent.Animate.walk.WalkAnim.AnimationId = "rbxassetid://14490337008" --CHANGE ANIMS
		Obj.Parent.Animate.idle.Animation1.AnimationId = "rbxassetid://14490292025"
		Obj.Parent.Animate.idle.Animation2.AnimationId = "rbxassetid://14490292025"
		Obj.Parent.Animate.jump.JumpAnim.AnimationId = "rbxassetid://14490288593"
		Obj.Parent.Animate.fall.FallAnim.AnimationId = "rbxassetid://14490284229"
	end
end

script.Parent.Touched:Connect(onTouched)
3 Likes

They revert on death because the game creates a new humanoid. If you want to bring those animations back in after they died, you can save animations to a table and when character added is called in a local script, reference those animations and load them in then

I see, so kinda a mix of what I did on both of my attempts… I’ll try that real quick.

1 Like

Alright, so I’m pretty new to scripting and unsure what problems I’m encountering. I took your advice and right now I’m working on the local script (I placed it inside of StarterCharacterScripts), but it doesn’t work, mind telling me what I’m doing wrong?

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://14490337008" --CHANGE ANIMS
		char.Animate.idle.Animation1.AnimationId = "rbxassetid://14490292025"
		char.Animate.idle.Animation2.AnimationId = "rbxassetid://14490292025"
		char.Animate.jump.JumpAnim.AnimationId = "rbxassetid://14490288593"
		char.Animate.fall.FallAnim.AnimationId = "rbxassetid://14490284229"
	end)
end)

You don’t have to necassarily don’t have to do it through the Animate Script, you can just do it via a local script and paste animations in the local script and call them in the local script (and make sure that their priority is right).

1 Like

When parenting a script to StarterCharacterScripts it will only show up after the player’s character has been loaded, so running plr.CharacterAdded inside of that kind of script will never really process anything. In theory you could just remove the PlayerAdded, and CharacterAdded functions and the char.Animate lines will process when the player’s character is loaded

char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://14490337008" --CHANGE ANIMS
		char.Animate.idle.Animation1.AnimationId = "rbxassetid://14490292025"
		char.Animate.idle.Animation2.AnimationId = "rbxassetid://14490292025"
		char.Animate.jump.JumpAnim.AnimationId = "rbxassetid://14490288593"
		char.Animate.fall.FallAnim.AnimationId = "rbxassetid://14490284229"

Are you doing the touched event in a local script, or inside of a server script?

I gather that it’s inside of a server script since the script itself seems to reference the part being touched a script.Parent. My only concern with doing what I’ve suggested is that it’d give all of your players the new animations, instead of just the players whom touched the part. What you’ll need to do is basically setup somewhere to let the script know that it should take these new animations based on if the player has in the past ever touched the part.

Inside of your script that performs the Touched Event, you can put in a table of players who have touched the part, and then setup a listener for if the player’s character gets reloaded.

I don’t believe this would be your most effective and performant way of doing this. I’d personally look at testing to see if applying the animation IDs work on a local script, like inside the StarterCharacterScript section, and if that does work (since it’s a local script vs server script, we want to verify that functionality does work, otherwise we need to make some modifications). If that does work, then you could make the Touched Event instead trigger a remote event, and then have a local script that listens to that event. The local script should be saved inside the StarterPlayerScripts so that it persists through death, and not reloads every time the player dies. Now inside of this local script, you have a variable that is just yes or no, that script should listen for character added, not player added since it’s always loaded on that player, and then inside of the character added function of that script, you would check if that variables and apply your animations based on the output of that variable, and the remote event listener inside of this script would change the value of the variable from no to yes once it’s triggered.

Sorry, I know this is a lot of information, especially for someone new. I can help you further if that doesn’t make sense

Sorry for going quiet on you, I had to sleep and then take care of school.
I tested your stuff out and it mostly works, I’m just a little lost on the variable. Could you give me a code example of where and/or how I’d use it? Right now the new localscript in starterplayer looks something like this:

local RS = game:GetService("ReplicatedStorage")
local animChangesEvent = RS.Remotes.animChanges

local player = game.Players.LocalPlayer

local value = false

player.CharacterAdded:Connect(function(char)
	animChangesEvent.OnClientEvent:Connect(function()
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://14490337008" --CHANGE ANIMS
		char.Animate.idle.Animation1.AnimationId = "rbxassetid://14490292025"
		char.Animate.idle.Animation2.AnimationId = "rbxassetid://14490292025"
		char.Animate.jump.JumpAnim.AnimationId = "rbxassetid://14490288593"
		char.Animate.fall.FallAnim.AnimationId = "rbxassetid://14490284229"
	end)
end)

No worries!

So looking through this there will need to be some slight changes but you’re on the right track!

The consideration we’re trying to accomplish is if the player joins the game, they receive the default animations, but once they touch a part then they will receive updated animations. Furthermore, once the player dies, we want to see them still maintain the animations set upon them.

local RS = game:GetService("ReplicatedStorage")
local animChangesEvent = RS.Remotes.animChanges

local player = game.Players.LocalPlayer

local value = false

local function UpdateAnimations() --Creating a new function that will control when the animations get updated
	local char = player.Character
	if char then --Safeguard in case something goes wrong with finding the Player's Character when called
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://14490337008" --CHANGE ANIMS
		char.Animate.idle.Animation1.AnimationId = "rbxassetid://14490292025"
		char.Animate.idle.Animation2.AnimationId = "rbxassetid://14490292025"
		char.Animate.jump.JumpAnim.AnimationId = "rbxassetid://14490288593"
		char.Animate.fall.FallAnim.AnimationId = "rbxassetid://14490284229"
	end
end

animChangesEvent.OnClientEvent:Connect(function() 
	value = true --Since we now have received the trigger, we know that from now on, future Characters Added will need to be updated
	UpdateAnimations() --When the event gets triggered, update the animations
end)

player.CharacterAdded:Connect(function(char)
	if value then --Checks if value is true, and if so applies the animations
		UpdateAnimations()
	end
end)
2 Likes

Eureka! I had to add a WaitForChild for the animate script so it could load in, but otherwise it works! I’m surprised such a simple premise caused me so much issue, thank you for all the help. I suppose I should try to understand more of the basics of studio before I start future ambitious projects, though.

1 Like

Having an ambitious project isn’t so bad! It can allow you to learn things you didn’t realize before!
Happy coding!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.