Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED!] Why does the anonymous function end when the function "lowerbeam" is called?

local topbeam=script.Parent.Parent.Beams.High
local beams=script.Parent.Parent.Beams:GetChildren()
local capturetime=5
local m=Instance.new("Message")
local giving=false

function givepoints()
	giving=true
	while wait(1) do
		if script.Parent.Parent.HoldingTeam.Value==Game.Teams["Storm Brigade"] then
			Workspace.SBPoints.Value=Workspace.SBPoints.Value+1
		elseif script.Parent.Parent.HoldingTeam.Value==Game.Teams.Raiders then
			Workspace.RaiderPoints.Value=Workspace.RaiderPoints.Value+1
		end
	end
end

local lowerbeam = coroutine.wrap(function()
    for i=topbeam.Size.X, 13, -1 do
        for x, y in pairs(beams) do
            y:Resize(Enum.NormalId.Right, -1)
            wait()
        end
    end
end)


script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player=Game.Players:FindFirstChild(hit.Parent.Name)
		if player then
			for i, v in pairs(beams) do
				v.BrickColor=player.TeamColor
			end
			for i, v in pairs(Game.Teams:GetChildren()) do
				if v.TeamColor==player.TeamColor then
					script.Parent.Parent.HoldingTeam.Value=v
				end
			end
			local beamheight=topbeam.Position.Y+(topbeam.Size.Y/2)
			local floaterheight=script.Parent.Parent.FortFloaters.FortFloaters.Position.Y-(script.Parent.Parent.FortFloaters.FortFloaters.Size.Y/2)-100
			for i=beamheight, floaterheight do --transition to the new fort color
				for x, y in pairs(beams) do
					y.Size=y.Size+Vector3.new(1, 0, 0)
					y.Position=y.Position+Vector3.new(0, 0.5, 0)
				end
				wait(capturetime/(floaterheight-beamheight))
			end
			for i, v in pairs(script.Parent.Parent.FortFloaters:GetChildren()) do
				if v:IsA("BasePart") then
					v.BrickColor=player.TeamColor
				end
			end
			m.Parent=Workspace
			m.Text="The Fort has been captured by the "..script.Parent.Parent.HoldingTeam.Value.Name..[[!
			Their soldier ]]..player.Name..[[ has breached the defenses and activated the capture point to take the fort!]]
			lowerbeam()
			wait(3)
			m:Remove()
			if not giving then
				givepoints()
			end
		end
	end
end)

Everything goes seamlessly the first time the touched is called. The second time though, I run into the same issue I did before: the touched function cuts off at line 57, but this time I get an error:

`16:22:14.631 - Workspace.CapturePoint.Trigger.Script:57: cannot resume dead coroutine

16:22:14.631 - Script 'Workspace.CapturePoint.Trigger.Script', Line 57

16:22:14.631 - Stack End`

1 answer

✓ Accepted answer

I'm not sure what objects the lowerbeam is affecting but you can try making that a coroutine

local lowerbeam = coroutine.wrap(function()
    for i=topbeam.Size.X, 13, -1 do
        for x, y in pairs(beams) do
            y:Resize(Enum.NormalId.Right, -1)
            wait()
        end
    end
end)

You dont need to change line 56 btw. Keep it as lowerbeam()

Keep in mind that the other stuff will run WHILE that function is running so you can add a variable called BeamLowered = false and when its done you can set it to true while the line AFTER lowerbeam() waits for that and it would look like this

while not BeamLowered do
	wait()
end

RE-EDIT I think if you wrap it in a function it will create instead of resume

function lowerbeam()
	local lowerTEHbeam = coroutine.wrap(function()
	    for i=topbeam.Size.X, 13, -1 do
	        for x, y in pairs(beams) do
	            y:Resize(Enum.NormalId.Right, -1)
	            wait()
	        end
	    end
	end)
	lowerTEHbeam()
end

6 comments

0

lowerbeam affects three parts, each with a cylinder mesh in them. I'll go try that...

0

I tried it, and now I get an error the second time the script is run, but the first time it works seamlessly. I got an error this time too, see the edited question.

0

check i think that'll work

0

ok, I'll go try this in a bit...

0

Will other things still run while the function is running?

0

thanks, it works perfectly, and other things run while it's running (which is just what I wanted it to do)!

This discussion is preserved as it was. New answers, comments, and votes are closed.