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??
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
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.
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.
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
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.
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
@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