Checking Room Availability

local List = script.Parent.ScrollingFrame
local Rooms = game.ReplicatedStorage.RoomData

while true do
	for i,v in ipairs(Rooms:GetDescendants()) do
		if v:IsA("StringValue") then
			for i,r in ipairs(Rooms:GetChildren()) do
				if r:IsA("BoolValue") then
					for i,b in ipairs(List:GetDescendants()) do
						if b:IsA("TextButton") and b.Text == r.Name then
							if v.Value ~= "" then
								b.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
								print("works here")
							elseif b.Text == r.Name then
								if v.Value == "" then
									b.BackgroundColor3 = Color3.fromRGB(0, 198, 0)
									print("Done")
								end
							end
						end
					end
				end
			end
		end
	end
end

I’m trying to make it so that if the value is not = to “” then it’ll change the room state to unavailable, but it keeps them on green even when I input a username as the value. Any help is appreciated :slight_smile:

What is the default color? It seems there’s a few cases where the button is not assigned a color.

Also, I heavily reccomend you use a dictionary instead of sorting through everything multiple times over.

1 Like

The default is green. How do I go about using a dictionary? Thank you for helping!

image
Also just for a better explanation, this is the UI and basically what’s supposed to happen is if the value has a username in it, then the colour of that specific room number turns red.

So a dictionary is basically a table, but you call values like you would with the properties of an instance.

Dictionary  = {
	First = 2;
	Apple = "green";
	Status = "red";
}
print(Dictionary.First) --2
print(Dictionary.Status) --red
print(Dictionary.Apple) --green

for k,v in pairs(Dictionary) do 
	print(k.." is "..v) --e.g: Status is red
end

But while writing this, I realized that it would actually be more efficient to just find the related values that mess with dictionaries. I imagine you are looping through every BoolValue and TextButton because you’ve set up this system in such a way there’s no way to find the related values without doing so.

For starters, name every TextButton a number in ascending order. The first one is named “1”, the second is “2”, and so forth. The we can assign an attribute (better version of value instances) to each room that tells us what ID it is. Using that ID, you can find the associated TextButton without having to loop through every single one.

--I assume you already have a function that creates the rooms, so just add this to it.
Rooms.Room1:SetAttribute("RoomID",1) --Set attributes like this in the code.
Rooms.Room1:SetAttribute("UsersName","")

for i,v in ipairs(Rooms:GetChildren()) do
	local RoomOwner = v:GetAttribute("UsersName")
	local ID = v:GetAttribute("RoomID") --Get the ID so we can find the textbutton.
	local TextButton = List[ID]
	--Now for your conditional statements.
	if RoomOwner  ~= "" then
		TextButton.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		print("works here")
	elseif TextButton.Text == RoomOwner  then
		if RoomOwner == "" then
			TextButton.BackgroundColor3 = Color3.fromRGB(0, 198, 0)
			print("Done")
		end--What if RoomOwner isn't nil?
	end
	--Note: I don't know what the values of 'r' or 'v' are meant to be in the original code, so you might have to change this up a bit.
end
1 Like
local List = script.Parent.ScrollingFrame
local Rooms = game.ReplicatedStorage.RoomData

for i,v in ipairs(Rooms:GetChildren()) do
	local RoomOwner = v:GetAttribute("Username")
	for i,u in ipairs(List:GetDescendants()) do
		if u:IsA("TextButton") then
			if RoomOwner  ~= "" then
				u.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
				print("works here")
			elseif RoomOwner == "" then
				u.BackgroundColor3 = Color3.fromRGB(0, 198, 0)
				print("Done")
			end
		end--What if RoomOwner isn't nil?
	end
end

I’ve done this now, however, it now sets both to red even though one of the rooms isn’t occupied.

Basically, I want it so if the attribute doesn’t have anything in it, then it will turn green.

There is also other scripts that will add the attributes etc based on how many rooms I have within a folder.

damn bro too much if statement. try checking if the code meets certain requirements before executing the final results.

u have messy code right there. not to be toxic or anything but saying what on my mind

I have solved my issue, I realised I forgot to put a return to ensure it doesn’t read through the entire script every time. Thank you for helping!

I have fixed this, I tend to write messy before making it more tidy after.

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