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
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.
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.
This probably won’t fix your main issue, but I noticed a few other minor issues with your script:
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.
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.
I’d recommend indenting properly. It’ll help you and the people helping you find errors in your script much faster.
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.
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
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