True or false check script not working

so im trying to finish this script where it checks if a boolvalue is true or false, problem is the script doesn’t even seem to get to the part where it does this??? I’m assuming there’s an error earlier in the script but sadly there was no error message. I’m thinking it has something to do with the math.random. Help would be appreciated!

local cubes = game.Workspace.Cubes:GetChildren()


local function OnPlayerAdded(player) 
	while wait(0.5) do
	local cube = cubes[math.random(1, #cubes)]
	
	if not cube.occupied.Value then
		cube.owner.name.text.Text = player.Name
		print("success")
		break
		else
	    print("retry")
	    end
	end
	end

You seem to have a while true do without any form of wait and it caused your script to timeout.
Perhaps use while wait() do

Are you sure there aren’t any errors? I can see a lot of potential problems, for example it’s called the .Occupied property and not .occupied, also you have an extra end in your if statments.

there is a wait in the else part and if the script gets the right value then the loop should break, but ill try that

Assuming “occupied” is an object of type BoolValue, you’d need to be checking if cube.occupied.Value == false. cube.occupied will always evaluate to being true so as long as the object exists.

EDIT: Terminology.

yes it is, but I tried it and it didn’t seem to work.

This probably won’t fix your main issue, but I noticed a few other minor issues with your script:

  1. The wait(0.5) is inside the if statement rather than outside of it, which means it’ll only wait if cube.occupied == false. This can lead to script timeouts if it’s equal to true.

  2. Also, the == false isn’t required. The not keyword converts truthy values (true, basically anything else) to false and falsey values (false, nil) to true. Which means the statement not cube.occupied automatically checks if cube.occupied == false, so you only need to do that instead of writing == false every time.

  3. I’d recommend indenting properly. It’ll help you and the people helping you find errors in your script much faster.

Yeah I just changed the first one, I might try the second one too.

I’m pretty sure the extra end is required.

Eeailer he didn’t have it setup as a function

local function OnPlayerAdded(player) 

He added it recently, I guess that end was for the function which he just added.

I know this is probably dumb, but have you called the OnPlayerAdded function? Because in the script you’ve given us it doesn’t seem you have, unless i’m missing something.

It’s been a function this whole time-

Yeah upon further inspection it might be the starting function that isn’t working.

I’m pretty sure it wasn’t, you even changed some stuff like wait(0.5), click this button to check if you want
image

Try adding this to the bottom of your script:

for i, v in pairs(game.Players:GetPlayers()) do 
OnPlayerAdded(v)
end

EDIT: Have you called the PlayerAdded event in the first place?

game:GetService(“Players”).PlayerAdded:Connect(OnPlayerAdded)

I know it was because I added it after a previous post.

I can’t really put it anywhere, it keeps telling me it’s wrong.


local function OnPlayerAdded(player) 
	print("works")
	while wait(0.5) do
	local cube = cubes[math.random(1, #cubes)]
	
	if not cube.occupied.Value then
		cube.owner.name.text.Text = player.Name
		print("success")
		break
		else
	    print("retry")
	
	
end
end
end
for i, v in pairs(game.Players:GetPlayers()) do 
onPlayerAdded(v)
end
local cubes = game.Workspace.Cubes:GetChildren()



local function OnPlayerAdded(player) 
	while wait(0.5) do
	local cube = cubes[math.random(1, #cubes)]

	print(cube.occupied.Value)
	if not cube.occupied.Value then
	
			cube.owner.name.text.Text = player.Name
			print("success")
			break
		else
		    print("retry")
	    end
	end
end


game.Players.PlayerAdded:Connect(OnPlayerAdded)

for i, v in pairs(game.Players:GetPlayers()) do 
	OnPlayerAdded(v)
end

Your script seemed to work, looks like I still have a bit more to learn with functions.

speaking of functions, math.random() uses “1” as the default lower limit. So you don’t have to specify that the lower limit is 1 each time, if you just give it one argument then it uses 1 automatically.

local randObject = tab[math.random(1, #tab)] -- works, but why do this
local randObject = tab[math.random(#tab)] -- looks cleaner