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:
-- 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.
-- 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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…