Coroutine.Wrap not Working

I know this is something stupid that I have done :sweat_smile: but I can’t figure out what it is. The script is supposed to cycle through all the players at the same time, but it does them one by one.

coroutine.wrap(function()
	for i, player in pairs(game.Players:GetChildren()) do 
		if player.Character then
			local person = player
			local startingpos = person.Character.PrimaryPart.CFrame
			print('looped')
			local params = RaycastParams.new()
			params.FilterDescendantsInstances = {person}
			params.FilterType = Enum.RaycastFilterType.Exclude
			local beam = workspace:Raycast(person.Character.PrimaryPart.Position,Vector3.new(0,-40,0),params)
			local Smasher
			local InvisSmasher
			if beam then
				player:SetAttribute('CanRun',false)
				player.Character.Humanoid.WalkSpeed = 10
				Smasher = Instance.new('Part',workspace)
				local quickveloc = person.Character.PrimaryPart.AssemblyLinearVelocity
				Smasher.Shape = Enum.PartType.Cylinder
				Smasher.Material = Enum.Material.ForceField
				Smasher.Orientation = Vector3.new(0,0,90)
				Smasher.Position = person.Character.PrimaryPart.Position + Vector3.new((quickveloc.X),0,(quickveloc.Z)) - Vector3.new(0,beam.Distance,0)
				Smasher.Anchored = true
				Smasher.Size = Vector3.new(3,80,80)
				Smasher.CanCollide = false
				Smasher.BrickColor = BrickColor.new('Gold')

				InvisSmasher = Instance.new('Part',workspace)
				InvisSmasher.Shape = Enum.PartType.Cylinder
				InvisSmasher.Transparency = 1
				InvisSmasher.Orientation = Vector3.new(0,0,90)
				InvisSmasher.Position = Smasher.Position
				InvisSmasher.Anchored = true
				InvisSmasher.Size = Vector3.new(20,80,80)
				InvisSmasher.CanCollide = false
				InvisSmasher.CanCollide = false
			end
		local groundclone = game.ReplicatedStorage.GroundSmash:Clone()
		groundclone.Parent = workspace
		groundclone:PivotTo(Smasher.CFrame * CFrame.new(Vector3.new(-50,0,0)) * CFrame.Angles(0,0,math.rad(-90)))--(startingpos - Vector3.new(0,50,0))
		local TweenService = game:GetService('TweenService')
		local part = groundclone.Hitbox 
		local info = TweenInfo.new(.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,true,0)
		local goal = {CFrame = groundclone.PrimaryPart.CFrame + Vector3.new(0,80,0)}
		local Tween = TweenService:Create(part,info,goal)
		task.wait(2.5)
		script.Swoosh:Play()
		task.wait(1.5)
		script.RockSmash:Play()
		Tween:Play()
		player.Character.Humanoid.WalkSpeed = 16
		player:SetAttribute('CanRun',true)
		local shove
		local touching = workspace:GetPartsInPart(InvisSmasher)
		for i,v in pairs(touching) do
			if v.Parent == person.Character then
				v.Parent.Humanoid:TakeDamage(60)
				shove = Instance.new("BodyVelocity")
				shove.Parent = v.Parent:FindFirstChild("HumanoidRootPart")
					v.Parent:FindFirstChild("Humanoid"):ChangeState(Enum.HumanoidStateType.FallingDown)
				shove.MaxForce = Vector3.new(1000000,1000000,1000000)
				shove.Velocity = Vector3.new(0,300,0)
				break
			end
		end
		task.wait(1)
		if shove then
			shove:Destroy()
		end
		groundclone:Destroy()
		Smasher:Destroy()
		InvisSmasher:Destroy()
	end
end
task.wait(5)
end)()

You’re not using it correctly, usually it takes a function as an argument and returns a wrapped coroutine that can be executed using coroutine.resume which in your code you’re immediately executing it, remove the () at the end of the line and store the wrapped coroutine in a variable, then, you can resume the coroutine later to execute it Xx

So an example would be like

local action = coroutine.wrap(function()
--blah blah stuffs stuffs
end)
action()

?

1 Like

Ahhhh so true! That is correct! Xx

3 Likes

Thanks so much!
blah blah extra words so it will let me post it blah blah

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.