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

Boolean is not triggering this part of the script?

game.Players.PlayerAdded:connect(function(plr)
	local strVal = Instance.new("StringValue",plr)
	strVal.Name = "Team"
end)

if script.ready == true then
	print("Game is ready to begin.")
	local amount = #game.Players:GetChildren()
	print(amount)
	if amount == 1 then
		print("Amount is 1.")
		for _, player in pairs(game.Players:GetPlayers()) do
			local randPlr = math.random(1,amount)
			local chosen = player[randPlr]
			print(chosen)
			local PLR = game.Players:FindFirstChild(chosen)
			PLR.Team = "Survivor"
		end
	end
end

This is my script here. The following does not execute for some odd reason.

	if amount == 1 then
		print("Amount is 1.")
		for _, player in pairs(game.Players:GetPlayers()) do
			local randPlr = math.random(1,amount)
			local chosen = player[randPlr]
			print(chosen)
			local PLR = game.Players:FindFirstChild(chosen)
			PLR.Team = "Survivor"
		end
	end

The bool value 'ready' inside of the script is true , but it does not execute the above. I cannot explain why the if value isn't being executed. Not even a guess.

Here's the script that actually sets the bool value to true, for a better understanding of the entire idea. This part works perfectly fine.

game.Players.PlayerAdded:connect(function()
	local players = #game.Players:GetChildren()
	if players == 1 then
		print(players.." players are ingame. Preparing match shortly.")
		script.Parent.TeamAllocate.ready.Value = true
	end
end)

Any help is appreciated. I've gotten back to coding after about a year and I have this killer game idea that people will love... this, obviously, correlates to it. ; -)

Also! Important player values are set to 1 for my own personal testing (since kon has no friends)

1 answer

Well, there are a few things to keep in mind. First off, the reason it does not run: You are checking to see if the bool is true at the start of the code. A better idea is to wait until the bool is true, which would look like this:

repeat wait() until script.ready.Value -- Making it wait until the VALUE is true, not the Instance

But since it acts like a trigger, you should make it able to run more than once. There is also use of unneeded code and certain things you overlooked, as outlined:

game.Players.PlayerAdded:connect(function(plr)
    local strVal = Instance.new("StringValue",plr)
    strVal.Name = "Team"
end)
local function Startgame() -- Called when a new game starts
	print("Game is ready to begin.")
	local amount = #game.Players:GetChildren()
	print(amount)
	for _, player in pairs(game.Players:GetPlayers()) do
		local randPlr = math.random(1,amount)
       	print(chosen)
       	local PLR = game.Players:GetPlayers()[randPlr]
	PLR.Team.Value = "Survivor" -- Change the VALUE
end
end

script.ready.Changed:Connect(Function() -- triggers once the value is changed
	if script.ready.Value then -- Checks f it's true.
		StartGame()
	end
end

You may be wondering why I did not check for the amount. Simple, really. You are setting the bool's value to true when the amount of players is exactly 1 (Which I don't understand why?) - And once it is true, the code runs. You don't need to check it twice. But here's the thing, you're looking for a random player, when the code runs with only 1 player. On top of that, there could be some issues. What I THINK you're trying to do is put them in teams, randomly. The code should look like this:

math.randomseed(tick())
local ListOfTeams = {"Survivor", "Other"} -- Put the names of the teams
for _, player in pairs(game.Players:GetPlayers()) do
	local ChosenTeam = math.random(1,#ListOfTeams)
	player.Team.Value = ListOfTeams[ChosenTeam]
end

The last thing you might wish to adjust is the time when you make it true. Currently, the script ONLY runs when there is exactly one player in the game. Also, this checks every time a player Joins. What I suspect you wish to do start once there are 2 or more players:

RoundTime = 100
while true do -- Runs every 100 seconds, since I'm assuming this is a Minigame script
	repeat wait() until #game.Players:GetPlayers() >1 -- Waits until there are 2 or more players before starting
	script.Parent.TeamAllocate.ready.Value = true
	--Add whatever you want
	wait(RoundTime)
end

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