I,v pairs loop fail [SOLVED]

Even with the script that I’ve implemented?

well thats why i put the mode link. but i will implement it into my script. i tried it before but it didnt work. sorry. I will try tho

give me a few minutes to put it in

Alright, and I’ll go ahead and try it anyway to see if more errors arise.

yeh just for context. i want it to spawn in both zones or more at the same time

There are a few errors; all caused from spawnzones being a table instead of an instance, so I’m here fixing those right now. They may not be spawning at the same time, but they are randomized so they do kind of spawn at the same time.

Another error is that the number isn’t changing for every spawn; it’s still giving the same instance each time.

Which is why @itsLevande had said that statement about task.spawn and coroutine. If we was to apply that to this code, it should work as we want and still randomize. (thank you again, @itsLevande for the explanation).

image

The new and working script:

wait(1)

local tweenservice = game:GetService("TweenService")
local tweeninfo2 = TweenInfo.new(0.5)
local tweeninfo1 = TweenInfo.new(1)
local spawnables = game.ReplicatedStorage.Spawnables
local num1 = 30 -- Amount that can be spawned in
local currency = "Visors" -- Currency given when visor is collected


for i,v in pairs(spawnables:GetChildren()) do
	task.spawn( function()
	local spawnzone = workspace:FindFirstChild("Spawnables"):GetChildren()
	local rZones = math.random(1, #spawnzone)
	local zTP = spawnzone[rZones]

	local spawnlimit = zTP:FindFirstChild("Spawned")
	local currencygiven = v:FindFirstChild("Value").Value
	
	print("loading ".. zTP.Name)

	while spawnlimit.Value < num1 do
		local deb = true
		
		local cloned = v:Clone()
		local creationtween = tweenservice:Create(cloned,tweeninfo2,{Transparency=0})
		
		cloned.Parent = zTP
		cloned.CFrame = zTP.CFrame * CFrame.new(math.random(-zTP.Size.X/2, zTP.Size.X/2),zTP.Size.Y,math.random(-zTP.Size.X/2, zTP.Size.X/2))
		creationtween:Play()
		
		cloned:FindFirstChild("1").Enabled = true
		spawnlimit.Value += 1
		
		if spawnlimit.Value > 25 then
			print("Too many visors")
		else
			print("Spawned visor for ".. zTP.Name)
		end

		cloned.Touched:Connect(function(hit)
			local collectedvalue = cloned:FindFirstChild("Collected")
			if collectedvalue.Value == false then
				local plr = game.Players:FindFirstChild(hit.Parent.Name)
				local leaderstats = plr:FindFirstChild("leaderstats")
				local points = leaderstats:FindFirstChild(currency)
				local removaltween = tweenservice:Create(cloned,tweeninfo1,{Transparency=1})
				
				print(hit.Parent.Name.." collected a visor from ".. zTP.Name )  
				
				collectedvalue.Value = true
				
				cloned:FindFirstChild("2").Enabled = true
				cloned:FindFirstChild("1"):Destroy()
				
				points.Value += currencygiven
				spawnlimit.Value -= 1
				removaltween:Play()
				
				wait(1)
				cloned:FindFirstChild("2").Enabled = false
				wait(1)
				
				cloned:Destroy()
			end


		end)
		wait(2)
		end
	end)
end
1 Like

the spawnables in replicatedstorage?

Check the newly edited post:

For me, they’re working just fine. It must be you.

accidentally deleted a post but they are switched
and they dont respawn

Though, I think it would be more easier if you had of just copied the colors from the ZTP and project it onto the visors.

im going to see if switching the colors and names of them both will work. then ill test it with 3 zones.

just to make it simplier, can u send the updated model to me

I didn’t modify anything else but the code, just copy paste the code I’ve put in the edited message.

just had to reorganize the folders, thanks.

1 Like

Oh, well your welcome! If you need any other help, you can PM me; good luck on your game!

Thanks, see you later soon I guess.