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

If multiple BoolValue.Value == true then, ? (Read for explanation)

Basically I have this script

script.Parent.MouseButton1Down:Connect(function()
		if Contracted.Value == false then
			Contracted.Value = false
		for i,v2 in pairs(game.Workspace.Contracts:GetChildren()) do
	if v2:IsA("Folder") then
		if v2.ID.Value == script.Parent.Parent.ID.Value then
			game.Workspace.Contracts.ContractCount.Value = game.Workspace.Contracts.ContractCount.Value - 1 
			jName.Value = script.Parent.Parent.CargoType.Text.Text
			jDestination.Value = script.Parent.Parent.Dropoff.EndPoint.Text.Text
			jPay.Value = script.Parent.Parent.Pay.Total.Text.Text
			p.PlayerGui.Hud.ContractMenu.ContractList.ScrollingFrame.ContractPage.Visible = false
			p.PlayerGui.Hud.ContractMenu:TweenPosition(UDim2.new(0.0, 0,1.2, 0), "Out", "Quad", .1, true)
			wait(.1)
			p.PlayerGui.Hud.ContractMenu.Visible = false
			game.Players.LocalPlayer.PlayerGui.Hud.Contracts.OpenClose.Debounce.Value = false
			local clone = game.ReplicatedStorage[jName.Value]:Clone()
			clone.Parent = game.Workspace.Trailers
			local tl = {}
			repeat
			tl = findRandomChild(game.Workspace.Destinations[script.Parent.Parent.Dropoff.EndPoint.Text.Text].TrailerLocations)
			until tl.Taken.Value == false
			if tl.Taken.Value == false then
				tl.Taken.Value = true
				clone:MoveTo(Vector3.new(tl.Position.X,tl.Position.Y + 2,tl.Position.Z))

Basically what happens is if you click the GUI, it will close the gui out, clone a model, and position it at a certain brick. With the idea of my game, I have to have multiple places that the brick can go, so I have a 'Folder' with the possible locations the brick can go to, and a BoolValue inside of those to see if the spot is 'Taken' or not. The script works great, but my only issue is with the Repeat. Once the locations are full, and it can no longer go anywhere else, the game just freezes, because its waiting for taken to become false, which won't happen.

I have a few ideas to solve this, but not sure how to go about it. One idea is to have a loop check to see if all the BoolValue's are true, and if so prevent script from running. Is there a way I can do this? Are there any other ways I could check the values, and if all are set to true (no more spots left) then end the script?

1 answer

✓ Accepted answer

Within your FindRandomChild function, instead of repeatedly checking children until one is available, I would create a table of available children. Then, you only have to choose a random one once, and there will no longer be the infinite loop problem:

local function GetAvailableDestinations(Destinations)
	local Available = {}
	for _,Destination in pairs(Destinations:GetChildren()) do
		if Destination.Taken.Value == false then
			table.insert(Available, Destination)
		end
	end
	return Available
end

Then, when you choose a destination:

local function GetRandomDestination(Destinations)
	local Available = GetAvailableDestinations(Destinations)
	if #Available > 0 then
		return Available[math.random(#Available)]
	end
end

which will return nil if there are no destinations left, or the randomly chosen destination out of the ones that are left.

5 comments

0

This would work if there weren't outside values affecting the taken position.

0

Basically I have a GUI menu that generates random cargo delivery contracts. When you select a contract it will spawn your cargo at one of the multiple "Spawn locations". Once the player is there and removes his cargo, it will change the Taken value back to false, to allow another cargo to spawn there.

0

Yes this will work with that, all it's doing is checking the Taken values.

0

It's been a month, but I learned a lot more and finally came back to this problem and your solution worked great. Thanks man.!

0

No problem, glad it worked out!

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