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?
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
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.
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!
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.
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.
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.