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

What is wrong with this loot spawning/despawning code?

I am not sure how to interpret what the Output said(stated after code). I could be adding too many ends or something, or adding too few or whatever. If I knew I wouldn't be asking. The name of the script is 'L00t'

while wait(10) do
	z = math.random(1,4)
	print(z)
---------------------------------------------------------------------------------------------------
	if z == 1 then
		a = game.Lighting.ScrapCorrodeA:Clone()
		a.Parent = game.Workspace
		wait(5)
			if a.Parent ~= game.Workspace then
			Print("ScrapCorrodeA was picked up!")
			else
			a:Remove()
			end
---------------------------------------------------------------------------------------------------
	elseif z == 2 then
		b = game.Lighting.PipeCorrodeA:Clone()
		b.Parent = game.Workspace
		wait(5)
			if b.Parent ~= game.Workspace then
			Print("PipeCorrodeA was picked up!")
			else
			b:Remove()
			end
---------------------------------------------------------------------------------------------------
	elseif z == 3 then
		c = game.Lighting.FabricA:Clone()
		c.Parent = game.Workspace
		wait(5)
			if c.Parent ~= game.Workspace then
			Print("FabricA was picked up!")
			else
			c:Remove()
			end
			end
--------------------------------------------------------------------------------------------------
	elseif z == 4 then --LINE 36
		d = game.Lighting.SpringA:Clone()
		d.Parent = game.Workspace
		wait(5)
		if d.Parent ~= game.Workspace then
			Print("SpringA was picked up!")
			else
			d:Remove()
		end
end
end

Here is what I got in Output just after testing: 20:26:11.356 - Workspace.L00t:36: 'end' expected (to close 'while' at line 1) near 'elseif'

3 comments

0

Missing 3 ends. (For all the else ifs)

0

You have an extra end in the third elseif (z == 3, extra on line 33 or 34.)

0

And I also realize that I duplicate a variable and refer to it later on.

1 answer

while wait(10) do
	z = math.random(1,4)
	print(z)

	if z == 1 then
		a = game.Lighting.ScrapCorrodeA:Clone()
	else if z == 2 then
		a = game.Lighting.PipeCorrodeA:Clone()
	elseif z == 3 then
		a = game.Lighting.FabricA:Clone()
	elseif z == 4 then
		a = game.Lighting.SpringA:Clone()
	end

	a.Parent = game.Workspace
	wait(5)

	if a.Parent ~= game.Workspace then
		Print("Item was picked up!")
	else
		a:Remove()
	end
end

That should help a bit. You had and extra end (as I stated in my comment), but you mainly had the same piece of code four times, so by using the if and elseifs only on the part that was different, I shortened the code and made it easier to understand.

Feel fee to comment with any questions, accept the answer and/or vote it up if you find this helpful. Thanks!

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