• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

Roblox Trouble temporarily disabling a player's custom walk animation (1 Viewer)

kevintdame

Newcomer
Joined
Dec 24, 2020
Messages
11
Reaction score
0
Hi. I have a game where certain players who click on a "ghost" button temporarily lose their walk / run animations so they they are able to float / fly around without their legs moving. I have an approach that works for some players but for others it doesn't work and I think it has something to do with their particular custom animation their avatar has. I have narrowed down to 2 approaches which I thought would work but each has problems.

Approach #1: Temporarily turn off all animations for players who click the "ghost" button and then turn it back on when they are no longer a ghost. Problem: This approach does not work for any players. Their legs still move as they move around the game (instead of the player gliding around). Here is the code I used:

Code:
-- Step 1. When player clicks on ghost button, triggering GhostEvent, turn them into a ghost---

makeAGhost = game.ReplicatedStorage.GhostEvent.OnServerEvent:Connect(function(player)
makeAGhost: Disconnect()

player.Character.Humanoid.HipHeight = player.Character.Humanoid.HipHeight + 10

for i, Child in pairs (player.Character:GetDescendants()) do
            if Child:IsA('Part') or Child:IsA('MeshPart') then
                if Child.Name ~= "HumanoidRootPart" then Child.Transparency = .85
                end
             end
end

local oldGhost = player.Character.Humanoid
local ActiveTracks = oldGhost:GetPlayingAnimationTracks()

for _,v in pairs(ActiveTracks) do
  v:Stop()
v.Destroy()
end

end)


-- Step 2. When done, turn them back into a normal player who's not a ghost, using the original custom walk and run animations captured in step 1 above ---

local Players = game.Players:GetPlayers()
local isGhost = false

for a,b in pairs(Players) do

local oldGhost = b.Character.Humanoid
local ActiveTracks = oldGhost:GetPlayingAnimationTracks()
for _,v in pairs(ActiveTracks) do
v:Play()
end

---

Approach #2: Change the players' walk and run animations to the "idle" animation and when done restore their original walk and run animations. Problem: This approach worked well for some players but for one player in particular I get an error saying: "WalkAnim is not a valid member of stringvalue [players name]" from the code in Step 1 below when I try to ascertain what their walk and run animations are before turning into a ghost so I can use it later to restore back.

Code:
-- Step 1. Before becoming a ghost, determine what their custom walk / run animation is so we can restore it later ---


local Players = game.Players:GetPlayers() -- checks the amount of players

for a,b in pairs(Players) do

originalwalkanim = b.Character.Animate.walk.WalkAnim.AnimationId -- THIS TRIGGERS THE ERROR
originalrunanim = b.Character.Animate.run.RunAnim.AnimationId

-- NOTE: I don't think this is causing the error above, but it also occurs to me now when looking at this that I am not saving this information anywhere for each player. I should probably should create a table right?


-- Step 2. When player clicks on ghost button, triggering GhostEvent, turn them into a ghost---

makeAGhost = game.ReplicatedStorage.GhostEvent.OnServerEvent:Connect(function(player)

makeAGhost:Disconnect()
player.Character.Humanoid.HipHeight = player.Character.Humanoid.HipHeight + 10

for i, Child in pairs (player.Character:GetDescendants()) do
            if Child:IsA('Part') or Child:IsA('MeshPart') then
                if Child.Name ~= "HumanoidRootPart" then Child.Transparency = .85
                end
             end
end

        player.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://507766666"
        player.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://507766666"

    end)


-- Step 3. When done, turn them back into a normal player who's not a ghost, using the original custom walk and run animations captured in step 1 above ---

local Players = game.Players:GetPlayers()
for a,b in pairs(Players) do

b.Character.Animate.walk.WalkAnim.AnimationId = originalwalkanim
b.Character.Animate.run.RunAnim.AnimationId = originalrunanim

---

I would greatly appreciate any guidance you can provide. thank you!
 
Last edited:
Top