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

Help with Value Detecting and TextLabel Text?

local hasroom = false
local debounce = true
local roomname = "Room"
while true do
	wait()
	debounce = true
	local plr = game.Players.LocalPlayer
	for i,v in pairs(game.Workspace.Rooms:GetChildren()) do
		local ownr = v:FindFirstChild("plrowner")
		if ownr then
			if ownr.Value == plr.Name then
				if debounce == true then
					hasroom = true
					debounce = false
					roomname = v.Name
				end
			else
				if debounce == true then
					hasroom = false
					debounce = false
				end
			end
		end
	end
	
	if hasroom == true then
		script.Parent.Text = "You're in room "..roomname
	else
		script.Parent.Text = "You're not in a room"
	end
end

So, there are 3 hotel rooms. In each hotel room is a value with the name of the player who owns it. In this GUI I want it to say what hotel room you're in if it detects a hotel room with your name in the owner value, or to say that you're in no hotel room if there isn't a hotel room with your name in the owner value. The issue is that it doesn't work all too well. It only works if you're in room 3 (the last room), but if you're in room 1 or 2 it always says you don't have a room. Some help please?

1 answer

✓ Accepted answer

It isn't working in rooms 1 or 2 is because of your else statement on line 17.

Your code makes it so if the current room iteration's owner is not the localplayer, it will put 'hasroom' to false. This is a problem because if the player's room is 1 or 2 the code will set 'hasroom' to true, go to the next room and see the player does not own it, then return 'hasroom' back to false.

This is the reason for it only working when you own room 3, because there are no rooms ahead to check. And therefore, the code cannot reset 'hasroom'.

So, to fix this.. basically remove the whole else statement from your code and you should be good.

P.S - debounce isn't necessary here. It just clutters the code since it isn't actually controlling anything.

local plr = game.Players.LocalPlayer
local hasroom = false
local roomname = "Room"

while wait() do
	for i,v in pairs(workspace.Rooms:GetChildren()) do
		local ownr = v:FindFirstChild("plrowner")
		if ownr then
			if ownr.Value == plr.Name then
				hasroom = true
				roomname = v.Name
				--The 'break' statement stops loops
				break
				--Since 'hasroom' is already true, break the loop
			end
		end
	end
end

if hasroom then
	script.Parent.Text = "You're in room "..roomname
else
	script.Parent.Text = "You're not in a room"
end

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