Is this a good idea?

I’m using a repeat until loop to repeat something 2 times, and each time in the loop i add a value and the loop stops when the value hits 2. However, sometimes in Roblox, the lag causes the code to break, causing a flurry of item duping.

repeat 

		local nextbotcandidate=math.random(1, #nextbottable)
		local nextbotchosen=nextbottable[nextbotcandidate]
		local clonednextbot=nextbotchosen:Clone()
		clonednextbot.Parent=workspace.CurrentNextbots
	
		nextbotcount+=1
	
		local nextbothum=clonednextbot:FindFirstChild("HumanoidRootPart")
		nextbothum.CFrame=nextbotspawnlocation.CFrame
	
	

		

		
		print("running nextbot chooser...")
	until nextbotcount==2
	nextbotcount=0

Edit: and sometimes, there would only be 1 nextbot in the folder, except its supposed to be 2??

4 Likes

its more practical to use a for loop but repeat is fine too
Just add a wait so your game doesn’t almost crash

for nextbotCount = 1, 2, 1 do

If you wanted to use a for loop

2 Likes

It still only chooses 1 nextbot and brings it into workspace, and it does not even set the humanoidrootpart cframe to the spawn cframe

for nextbotCount = 1, 2, 1 do
		wait()
	print("nextbotspawneventactivated")
	local nextbotcandidate=math.random(1, #nextbottable)
	local nextbotchosen=nextbottable[nextbotcandidate]
	local clonednextbot=nextbotchosen:Clone()
	clonednextbot.Parent=workspace.CurrentNextbots

	nextbotcount+=1

	local nextbothum=clonednextbot:FindFirstChild("HumanoidRootPart")
	nextbothum.CFrame=nextbotspawnlocation.CFrame






		print("running nextbot chooser...")
	end
	nextbotcount=0

Remove the wait() that was only meant for the repeat loop sorry

The only thing I can see wrong with the script is the variables not referencing what you may think they are
Make sure “running nextbot chooser…” prints Twice

Unfortuately, the print does not print.

for nextbotCount = 1,2,1 do
		nextbotcount+=1
	print("nextbotspawneventactivated")
	local nextbotcandidate=math.random(1, #nextbottable)
	local nextbotchosen=nextbottable[nextbotcandidate]
	local clonednextbot=nextbotchosen:Clone()
	clonednextbot.Parent=workspace.CurrentNextbots
	
	local nextbothum=clonednextbot:FindFirstChild("HumanoidRootPart")
	nextbothum.CFrame=nextbotspawnlocation.CFrame
	
		print("running nextbot chooser...")






		
	end
	nextbotcount=0

It does the exact same thing mentioned above. The spawnlocation is mentioned correctly.

What?
Your code is not going to run differently because of lag

Idk lol, im still a beginner coder. All im trying to do is to control the amount of nextbots being spawned, but the repeat loop does not work. The code could break because of network latency (my thoughts), but since you are a more experienced scripter, i’ll follow your advice.

Does this give you a better result?

for nextbotCount = 0,2 do
   print("nextbotspawneventactivated")
   local nextbotcandidate=math.random(1, #nextbottable)
   local nextbotchosen=nextbottable[nextbotcandidate]
   local clonednextbot=nextbotchosen:Clone()
   clonednextbot.Parent=workspace.CurrentNextbots
   
   local nextbothum=clonednextbot:FindFirstChild("HumanoidRootPart")
   nextbothum.CFrame=nextbotspawnlocation.CFrame
   
   	print("running nextbot chooser...")






   	
end

That breaking is probably due to your nextbotcounter thingy being set to 1 when you create the variable, but a for loop is still better

It still only puts 1 nextbot into the folder

Network latency should not affect your code. Your code is either running on the server, or on the client, depending on what script you are using. Unless you use Parallel Lua, code always runs in a controlled order (even code from different scripts run one after the other).
Lag does not change the order in which code runs, only how fast it runs.
Even remote functions and remote events are made so that, whatever you send to the client/server will arrive in the same order it was sent.

so how should I set this up? Do you know how to?

Make sure it prints the correct amount of times, if it does then there must be the same amount of bots as prints. Otherwise, I don’t see anything wrong with it. It is possible that the boths are all being spawned at the same space and if they are anchored, they will be inside each other

Make sure you don’t have errors and stuff

Do you know how to add better constraints than just a repeat until loop? for loops don’t seen to work based on @samuelissad 's code.

There are no errors, but when I play the game it (somehow) bypasses the constraint i created.

What do you mean by constraint?

@samuelissad’s code is quite good, using a for loop is probably the best solution in this case (but please but a space on both sides of the = sign lol, my eyes are bleeding (would be more readable))

When running @samuelissad 's code, it says “Attempt to index nil with cframe”, which means that cframe does not exist. The repeat code does not have any errors, but NextbotCount does not reset to 0.

What I mean by constraints is how to make sure that the amount of nextbots in the folder is not greater than 2?

Attempt to index nil with cframe doesn’t mean that cframe doesn’t exist, it means that, whatever you are trying to access the cframe property of, is nil, in this case, nextbothum is nil, clonednextbot:FindFirstChild(“HumanoidRootPart”) was not able to find a part named HumanoidRootPart and thus returned nil (that is what FindFirstChild does)

To make sure that there are no more than 2 bots in the folder, you can do folder:GetChildren() to get a table containing all the childrens of the table. By getting the lenght of that table, you can find how many bots are in that table and stop the loop, or you can just make sure the loop only runs twice, if that works well with your other code

I took the code and adapted it to work with simple parts and it works just fine
image
image

thank you very much @Tomi1231 , @samuelissad and @TheParkourGamer10 for the help! I just added in a wait and a debounce for other constraint measures.

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