I need help with a little for loop issue. I have a system thats supposed to loop through all players and give them a body velocity to shoot back from an object. And everything works, exept for the fact that only one player gets flung back instead of all players. Only one player gets chucked back…
Heres the code:
for i,v in pairs(game.Players:GetChildren()) do
local charToPush = v.Character or v.CharacterAdded:Wait()
local HRP = charToPush:WaitForChild("HumanoidRootPart",math.huge)
local dir = (HRP.Position - newEffect.Position).unit --direction to push them in
local pushForce = newEffect.PushbackPower --Force character is being pushed at
local pushDuration = newEffect.PushbackDuration --Amount of time the force is applied
local animation = charToPush.Humanoid:LoadAnimation(game.ReplicatedStorage:WaitForChild("Clones").FloatAnimation)
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BV.Velocity = dir * pushForce
BV.Parent = HRP
animation:Play()
local slowDown = TweenService:Create(BV, TweenInfo.new(pushDuration, Enum.EasingStyle.Linear), {Velocity = Vector3.new()})
slowDown:Play()
wait(pushDuration)
local BP = Instance.new("BodyPosition")
BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BP.Position = HRP.Position
BP.Parent = HRP
BV:Destroy()
end
This is definitely the problem.
One way I get around that is by using coroutine i think you can use delay too.
for i,v in pairs(game.Players:GetChildren()) do
coroutine.wrap(function()
local charToPush = v.Character or v.CharacterAdded:Wait()
local HRP = charToPush:WaitForChild("HumanoidRootPart",math.huge)
local dir = (HRP.Position - newEffect.Position).unit --direction to push them in
local pushForce = newEffect.PushbackPower --Force character is being pushed at
local pushDuration = newEffect.PushbackDuration --Amount of time the force is applied
local animation = charToPush.Humanoid:LoadAnimation(game.ReplicatedStorage:WaitForChild("Clones").FloatAnimation)
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BV.Velocity = dir * pushForce
BV.Parent = HRP
animation:Play()
local slowDown = TweenService:Create(BV, TweenInfo.new(pushDuration, Enum.EasingStyle.Linear), {Velocity = Vector3.new()})
slowDown:Play()
wait(pushDuration)
local BP = Instance.new("BodyPosition")
BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BP.Position = HRP.Position
BP.Parent = HRP
BV:Destroy()
end)()
end
Hey, if you needed the code to be updated you could’ve just asked instead of making a new thread! Regardless:
for i,v in pairs(game.Players:GetChildren()) do
local charToPush = v.Character or v.CharacterAdded:Wait()
local HRP = charToPush:WaitForChild("HumanoidRootPart",math.huge)
local dir = (HRP.Position - newEffect.Position).unit --direction to push them in
local pushForce = newEffect.PushbackPower --Force character is being pushed at
local pushDuration = newEffect.PushbackDuration --Amount of time the force is applied
local animation = charToPush.Humanoid:LoadAnimation(game.ReplicatedStorage:WaitForChild("Clones").FloatAnimation)
local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BV.Velocity = dir * pushForce
BV.Parent = HRP
animation:Play()
local slowDown = TweenService:Create(BV, TweenInfo.new(pushDuration, Enum.EasingStyle.Linear), {Velocity = Vector3.new()})
slowDown:Play()
delay(pushDuration, function()
local BP = Instance.new("BodyPosition")
BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BP.Position = HRP.Position
BP.Parent = HRP
BV:Destroy()
end)
end
Also I’d appreciate if you marked my post from the other thread as solution