How to run animation of a player with a GUI button?

Hi, I have a button in GUI and I want it to run a Player animation.

I already tried a bindable function where the script is in StarterCharacterScripts but it doesn’t let me have access, it tells me that the function is not a member of the Player.

Also try a RemoteFunction and use a script in ServerScriptService but the ContextActionService = game: GetService (“ContextActionService”) does not work there.

Does anyone know how to do that or where I have to call the animation for the button to work?

Thank you.

4 Likes

What’s the code your using? We’ll be able to resolve this faster if we have more to work off of.

what I would do is fire your animation id code like so:

script.Parent.MouseButton1Click:Connect(function()
 game.ReplicatedStorage.RemoteEvent:FireServer("Animation Id")

end)

“Animation Id” is your animation id.

and then the script that receives it has something like this:

    game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,animationID)
 local animation = Instance.new("Animation")
 animation.AnimationId = "rbxassetid://"..animationID

local loadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
 loadedAnimation:Play()
end)

I recommend naming your remote event to your anim name.

3 Likes

usually scripting animations does not have to be so difficult, sometimes you need to look for easier solutions.

Thanks but I need the animation to run with a GUI button, the problem is that I don’t know how to access the animation, I want it for mobile.

yes, insert the fire script in the GUI button, when a player presses it, it will fire the animation script and play your animation. Here is a article about remote events if you do not understand:

Gui buttons work for mobile too when using that Mousebutton1Click Event.

I use this code to access the animation from the button but the problem is this line
local ContextActionService = game: GetService (“ContextActionService”)
does not work, its value is null

game.ReplicatedStorage.RFagachar.OnServerInvoke = function (player, num)

local Humanoid = player.Character:FindFirstChild('Humanoid')
local Animation = Instance.new("Animation", player.Character)

Animation.AnimationId = "rbxassetid://2846919658"
local Animate = Humanoid:LoadAnimation(Animation)

local flag = 1

local ContextActionService = game:GetService("ContextActionService")
local FREEZE_ACTION = "freezeMovement"

local mochila = game.Players.LocalPlayer.Backpack
local espada = mochila:FindFirstChild("Sword")

if flag == 1 then

	-- Stop all playing animations
	local AnimationTracks = Humanoid:GetPlayingAnimationTracks()

	for i, track in pairs (AnimationTracks) do
		track:Stop(0)
	end

	Animate:Play()
	Humanoid.JumpPower = 0
	
	--**freeze player
	ContextActionService:BindAction(
	    FREEZE_ACTION,
	    function()
	        return Enum.ContextActionResult.Sink 
	    end,
	    false,
	    unpack(Enum.PlayerActions:GetEnumItems())
	)

	if espada ~= nil then
		espada.ManualActivationOnly = true
	end
	
	
	flag = 2
else
	
	
	if espada ~= nil then
		espada.ManualActivationOnly = false
	end
	
	--**unfreeze player
	ContextActionService:UnbindAction(FREEZE_ACTION)

	Animate:Stop()
	Humanoid.JumpPower = 50
	flag = 1
end

end

do you have to do it this complicated? what type of script are you using for this?

ok then if I use the Mousebutton1Click Event in the gui button also works?

yes, if you want I can probably show a video example. It is also a lot more simple then your way.

yes please :grin:
I’ve spent a lot of time trying to figure this out.

Here is a pic of my fire event:

LocalScript%20-%20Roblox%20Studio%2010_21_2019%209_46_45%20PM

here is the video:

sorry it took little bit to set this up.
if you need anything else ask me.:grin:

1 Like

Thank you very much, I have to read about fireserver. I didn’t know about that.

2 Likes

just check through all of the stuff I talked about above, I am a pretty descent (I would not say advanced) GUI scripter, so you can come to me if you have anymore related problems with GUI scripting.:grin:

btw I do not want to be rude or anything, but if there is a solution make sure to check it off as one, so people do not get so confused about what the answer is and not try to answer your question after it was already answered. Thanks!:slightly_smiling_face:

1 Like

Quite simple, actually.
You have a LocalScript inside your GUI button with code along the lines of this.

script.Parent.MouseButton1Click:Connect(function()
       local Anim =  game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animationid)
       Anim:Play()
end

Animations can be loaded and played locally and they will replicate to the server automatically. No need for remotes.

(EDIT): Looking at the security of this, it might be safer to run it on serverside however, because exploiters might be able to abuse the system to run less than appropriate animations.

5 Likes

Oh that’s right! I forgot about that, but my way might work better for her to learn about remote events because those are pretty important with game making.Thanks for the reminder though @marfit.:grin:

However, having to pass this through RemoteEvents is inefficient and slower. It’s better to learn about efficiency early on so you don’t have issues with it later. It’s also far simpler my way.

that’s true, I just would like for her to learn about remote events too. It doesn’t matter either way, both scripts work and we shouldn’t argue about it. P.S (we are getting off topic, lets try and focus on the topic).

If the Humanoid object or an AnimationController object contains an Animator object inside of it, everything local sided that fires an animation to that object will replicate onto the server. Following this, you’ll only need to fire the GUI’s MouseButton1Click event and play the animation track through the function.

Your variable FREEZE_ACTION is only defined by a string, so it only appears in output. Fix that and hopefully it will work

1 Like