I have a code that plays a knockdown when you get below 10 health. Also it makes you able to be picked by another player by pressing K. The code works, but it’s finicky, wondering if there was a more efficient way to do this?
I know repeat loops are pretty horrific sometimes, is there something I can replace them with?
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
repeat wait() until player.Character
local Character = player.Character
local track = Instance.new("Animation")
track.AnimationId = "rbxassetid://6845922693"
local anim = Character.Humanoid:LoadAnimation(track)
local track2 = Instance.new("Animation")
track2.AnimationId = "rbxassetid://6845883493"
local anim2 = Character.Humanoid:LoadAnimation(track2)
print(Character.Name)
while true do
wait(0.5)
repeat wait(0.5)
until Character.Humanoid.Health <= 10
local knocked = Instance.new("BoolValue")
knocked.Name = "Knocked"
knocked.Parent = Character
game.Debris:AddItem(knocked,20)
local hit = Instance.new("BoolValue")
hit.Name = "Hit"
hit.Parent = Character
game.Debris:AddItem(hit,20)
anim:Play()
anim2:Play()
Character.HumanoidRootPart.Anchored = true
wait(20)
if anim2 then
anim2:Stop()
Character.HumanoidRootPart.Anchored = false
end
end
end)
end)
Any help is appreciated, thanks!