I am trying to figure out how I can change my script from using c-frame to playing an animation. Does anyone know how I would be able to do that?
Code
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local equipped = false
local up = false
local shield = script.Parent.Parent
local handle = script.Parent.Parent:FindFirstChild("Handle")
---------------------CODE BELOW--------------------------
print("Step0")
player:GetMouse().KeyDown:connect(function(k)
if not character:findFirstChild("Left Arm") then return end
if not character:findFirstChild("Torso") then return end
if not character.Torso:findFirstChild("Left Shoulder") then return end
local lsh = character.Torso:findFirstChild("Left Shoulder")
if k == "q" then --Spawns the Shield to the Left Arm
equipped = not equipped
up = false
lsh.Part1 = character["Left Arm"]
print("Step1")
if equipped then
for n,i in pairs(shield:GetChildren()) do
if i.Name ~= "Handle" and i:IsA("BasePart") then
local nw = Instance.new("Weld",i)
nw.Part0 = handle
nw.Part1 = handle
nw.C0 = handle.CFrame:inverse()
nw.C1 = i.CFrame:inverse()
end
end
handle.Anchored = false
print("Step2")
shield.Parent = character
shield:MakeJoints()
local nw = Instance.new("Weld",character["Left Arm"])
nw.Part0 = character["Left Arm"]
nw.Part1 = handle
nw.C1 = CFrame.new()
nw.Name = "LeftGrip"
else
if character["Left Arm"]:FindFirstChild("LeftGrip") then
character["Left Arm"].LeftGrip:remove()
end
shield:remove()
print("Step3")
end
elseif k == "e" and equipped then --Raise Shield
up = not up
if up then
lsh.Part1 = nil
local w = Instance.new("Weld",shield)
w.Part0 = character.Torso
w.Part1 = character["Left Arm"]
w.C1 = CFrame.new(1.2,-.25,0.25) * CFrame.fromEulerAnglesXYZ(math.rad(-75),math.rad(55),0)
else
for n,i in pairs(shield:GetChildren()) do
if i.ClassName == "Weld" then
if i.Part0 == character.Torso then
i.Parent = nil
end
end
end
lsh.Part1 = character["Left Arm"]
end
end
end)
Animations are loaded into the humanoid using a localscript. From there you can play or stop them, there really isn’t much to it.
The following example demonstrates the above (assume certain parts are variables even though undefined in the example).
-- Main
Tool.Activated:Connect(function(Mouse) -- Fires when the player clicks w/ the tool.
local AnimTrack = Humanoid:LoadAnimation(AnimationPath) -- i.e script.Parent.Animation, loads animation into humanoid.
AnimTrack:Play() -- Plays animation
end)
Tool.Unequipped:Connect(function(Mouse) -- Fires when player deselects the tool.
Animation:Stop() -- Stops animation
end)
Personally, I like to preload my animations so they’re more smooth to begin with, which you can do by calling ContentProvider:PreloadAsync() on the animation.
To integrate it into your tool, you’ll detect when you want the animation to play or stop and follow the same steps from above, however if you’re still unsure I wouldn’t mind giving you a hand with that.
What I’m trying to do is change the c-frame in my shield to play as an animation. What my shield does is, if you press the key Q, it spawns on your left arm. Then the key E raises it, basically I am trying to play an animation when you press E instead of a c-frame. Can I still play an animation inside the local script even though it’s not a tool?
Yes, you still can. My above reply was just assuming that you were using a tool, however as long as you’re calling it all form a localscript it should be fine.
Obviously you have to make the animations though, and I have no idea how to go about that, I have an animator in the studio I’m in who just ships each animation to me.
As long as you’re able to create the animations, you’d be able to replace the cframing with them.
Yeah, so just like in the example I posted above, use UserInputService to detect when a player presses a key and then play the animation.
Example:
-- Variables
local UserInputService = game:GetService('UserInputService')
-- Main
UserInputService.InputBegan:Connect(function(Input, GameProcessed)
if not GameProcessed then -- Player isn't typing
if Input.KeyCode == Enum.KeyCode.Q then -- If player presses Q
Animation:Play()
end
end
end
I don’t really see the point of using a script since it replicates, and nice examples. As long as before :LoadAnimation() you’ve made sure the character isn’t parented to nil.
You would use a script if it’s running on the server, rather than firing a remote event to the client to play the animation. So basically, you can run the animation client-side or server-side.
In this case calling it from the client is better because he is using UserInputService to detect when a key is pressed. Since it’ll replicate, playing the animation on the server is counter-productive.
It is because of this that I’d always recommend playing animations on the client, that way you don’t need to network from the client to the server which due to latency would feel less responsive for the player.
With that being said, however, if for some reason you’re detecting user input or certain relevant gameplay scenarios from the server to begin with, then playing the animation on the server would most likely be a better solution.