Server event attack only working one player at a time rather than all at once?

This is hard to explain in a title, but I have a button that one person in the server has access too and when they click it, it fires a remote event to the server to play an attack, in this case, a big hand will spawn in front of every player and flick them out of the map all at once.

My problem is that for some reason it only works half of the time and when it does work, it spawns the giant hand by each player and plays the full flick sequence one player at a time, rather than all players at once like I need.

Another note, this event will only effect players who are not the person who can press the button as well as not in the safe zone (there is a bool value in every player to check if they are in the safe zone or not).

Here is my script so far that controls the attack in ServerScriptService:

local rep = game:GetService("ReplicatedStorage")
local event = rep:FindFirstChild("ControlPanelEvents")
local models = rep:FindFirstChild("ControlModels")

event.MightyFlick.OnServerEvent:Connect(function()
	print("flicktime")
	for i, v in pairs (game.Players:GetPlayers()) do
		if v.Name ~= rep.CurrentMaster.Value and v.Character:FindFirstChild("SafeZone").Value == false then
			------hm nothing
			v.Character.HumanoidRootPart.Anchored = true
			local flicker = rep.ControlModels:FindFirstChild("MightyF"):Clone()
			flicker.Parent = workspace
			flicker:MoveTo(Vector3.new(v.Character.HumanoidRootPart.Position.X, v.Character.HumanoidRootPart.Position.Y, v.Character.HumanoidRootPart.Position.Z + 4))
			wait(1)
			flicker.One.Transparency = 1
			flicker.Two.Transparency = 0
			--play sound
			flicker.Two.slap:Play()
			--FLYY AWAY
			v.Character.HumanoidRootPart.Anchored = false
			local BodyVelocity = Instance.new("BodyVelocity", v.Character.HumanoidRootPart)
			BodyVelocity.Velocity = Vector3.new(0,math.huge^math.huge,-math.huge^math.huge)
			BodyVelocity.MaxForce = Vector3.new(0,50000,-50000)
			wait(0.5)
			BodyVelocity:Destroy()
			--remove and die
			wait(1.2)
			flicker:Destroy()
			v.Character.Humanoid.Health = -500
		end
	end	
end)

Because it hasn’t finished to one player. A loop can be in order, but it is always synchronous. That’s why when you do a numerical loop, it goes in order but doesn’t print at the same time.

To fix your problem, just put a coroutine.wrap() after the for i,v in pairs....

2 Likes

Wow I never knew about coroutines, it works perfectly! Thank you!

1 Like