I’ve been stuck on this problem, left, came back tried to solve it in multiple ways and studied this problem ALOT. Came back to it multiple times and I’ve come to the conclusion that I lack the understanding of something.
I feel like it’s a super simple solution but again something I just don’t know.
I’ll be as descriptive as possible.
I want to play an Animation. In this case an Aiming animation.
This AnimationID changes every time the tool is equipped in another script.
Here are the things to keep in mind that I looked into but didn’t help or Don’t know how it can help?
(-) Avoiding wait() and why
(-) changing priorities ‘this doesn’t fix the problem because it still plays a new animation with every time the function is ran’
What I think might solve this?
(-) Adding some sort of :Wait()
on character.Values.Aiming.Changed
or AnimationId.Changed
inside the function. But not sure how exactly?
My code in steps
(1) - where the Animations are stored.
(2) - where Animations are changed depending on the weapon.
^ everything above works perfectly, its just to show everything ^
(3) - where Animations are played. << (problem)
–/(1)/ Module script with the weapon ;; Where ANIMATION-IDs are stored
local Weapons =
["Pistol 1"] = {
["animations"] = {
["aiming"] = "rbxassetid://9087186774";
};
};
["Pistol 2"] = {
["animations"] = {
["aiming"] = "rbxassetid://9103230309";
};
};
}
return Weapons
–/(2)/ Inside the tool
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local moduleWeapons = require(ReplicatedStorage.Modules.moduleWeapons)
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = script.Parent
local weaponName = tool.Name
--/!!/ TOOL EQUIPPED
tool.Equipped:Connect(function()
--/!!/ Grabs the full animation link
local animationID = moduleWeapons[weaponName].animations.aiming
--/!!/ I have a Animation instance already in "character.Animations.currentWeapon.aiming."
character.Animations.currentWeapon.aiming.AnimationId = animationID
end)
–/(3)/ Inside “StarterCharacterScripts” as "Animate"
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animationsFolder = character.Animations.currentWeapon
character.Values.Aiming.Changed:Connect(function(result) --/!!/ In another script changes the values to true / false when mouseB2 is up or down
local aiming = humanoid:LoadAnimation(animationsFolder.aiming) --/!!/ Loads a new animation evertime ran
if result == "true" then
aiming:Play()
else
aiming:Stop()
end
--/!!/ I want to stop the animation on a :Wait() until result == "false" in this same function.
end)
I know that every time the function
is ran, it will run a whole new
local aiming = humanoid:LoadAnimation(animationsFolder.aiming)
so calling aiming:Play()
or aiming:Stop()
will only play or stop the animation in the same function that was ran again.
Here’s a video
-- aiming - Client - Animate:28
-- aiming (x2) - Client - Animate:28
--.....and so on (+1 every time the animation is played again.)
as you can see in the Output using my debugging to get all playing animations and print them. Aiming is being played over again. Because of defining the LoadAnimation
in the function.
and I know I can just do
for i,Track in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
Track.Stop()
end
and be done with it… but I don’t want to depend on a for loop every time I want to play every animation. This is a bad idea to just have these laying around unless I know for sure there’s no solution.
What I don’t understand exactly is why this doesn’t work, and I also tried::
character.Values.Aiming.Changed:Connect(function(result)
aiming:Stop() --/??/ Doesn't work
local aiming = humanoid:LoadAnimation(animationsFolder.aiming)
if result == "true" then
aiming:Play()
else
aiming:Stop()
end
end)
and
local aiming = humanoid:LoadAnimation(animationsFolder.aiming) --/!!/ animation
character.Values.Aiming.Changed:Connect(function(result)
--/!!/ aiming never changes to the new animationID because its outside above this function
--/!!/ so the one above the function will be the only one thats going to be played
if result == "true" then
aiming:Play()
else
aiming:Stop()
end
end)
More laying it out exactly what I think a solution would be?..
character.Values.Aiming.Changed:Connect(function(result) --/!!/ ok playing function
local aiming = humanoid:LoadAnimation(animationsFolder.aiming) --/!!/ loading animation
if result == "true" then --/!!/ result is true lets play the animation
aiming:Play() --/??/ okay we are playing the animation, but... when do you want this to stop?
else
aiming:Stop()
end
--/!!/ I want this to stop if AnimationId changes, OR...result of the function / function is ran again. Stopping this animation.
--/!!/ Maybe using :Wait() somehow on AnimationId changed?
end)
or
character.Values.Aiming.Changed:Connect(function(result)
--/!!/ Something here to stop the last animation:: that was declared from the last time this function ran
local aiming = humanoid:LoadAnimation(animationsFolder.aiming)
if result == "true" then
aiming:Play()
else
aiming:Stop()
end
end)
Any suggestions are very helpful!