Need help assigning players turns for board game

Hi! If you could help me that would be great! Right now I am making a board game and I am wondering how I would assign players turns. For example, one player can roll a die. Then after they roll it, then the next player can do it, etc.

wait(6)

while true do
	p = game.Players:GetChildren()
	print(#p)
	print(p)
end

I dont really have a script. :pensive:

I am very stuck if someone could help that would be nice thanks!

You might be able to use a BoolValue or a StringValue to keep track of who’s turn it is.

Sadly I don’t know how to choose the correct player to assign to the bool or string value.

um idk i guess a table would work,

local order = {}

for i, player in pairs (game:GetService("Players"):GetPlayers()) do
table.insert (order,player)
end

Use a for loop

for _,plr in pairs(game:GetService(“Players”):GetPlayers()) do
-dice code
end

First, I would create the turn order in the start of the game, by adding each player that will play into a dictionary, I would also have a variable which says the CurrentTurn.

local TurnOrder = {}
local CurrentTurn = 1

function AddPlayers()
	for i,name in pairs(game.Players:GetPlayers()) do
	TurnOrder[i] = name
	end
end

By doing that the TurnOrder would be something like this:

TurnOrder[1] = Isaque232
TurnOrder[2] = killerdolophin2000
TurnOrder[3] = bobthepants123

After that, on the starting of the game I would check if CurrentTurn exists on the TurnOrder dictionary, which prevents the game from infinitely continue It’s turns even when they don’t have any players on them.

If CurrentTurn does not exist on the dictionary I would set it back up to 1, which basically resets and goes to the starting player.

To get the Player I would simply do: local Player = TurnOrder[CurrentTurn] which would return the value, which would be the player, with this I would be free to do anything to the player!

At the end of the turn I would simply add +1 to the dictionary so it goes to the next player in TurnOrder, and loop the whole thing so the game would keep going.

The script would be basically this:

local TurnOrder = {}
local CurrentTurn = 1

function AddPlayers()
	for i,name in pairs(game.Players:GetPlayers()) do
	TurnOrder[i] = name
	end
end

function StartTurn()
	if not TurnOrder[CurrentTurn] then 
		CurrentTurn = 1 
	end
	local Player = TurnOrder[CurrentTurn])
	
	--Do something to the player here!

	CurrentTurn += 1
	StartTurn() -- Starts the turn again!
end

AddPlayers() -- Add players to TurnOrder
StartTurn() -- Start the game!

This is just the idea of turns I’ve made up, of course you’re gonna need to change it, so it suits your game.

Hopefully this helps!

How would I add print functions to see the values in the TurnOrder Table?

I’m pretty sure this should work:

function SeeTable()
	for i,v in pairs(TurnOrder) do
		print(i,v)
	end
end

SeeTable()

Remember to mark the solution once It’s been answered!

image

It lags my game a lot is there a way to reduce it?

Yeah because there’s no wait() anywhere in the script, so it’s the same thing that happens when you use while true do without any wait.

You can easily fix that by adding any cooldown inside the StartTurn function, but please don’t use the script completly raw, instead change it so it suits your gameplay!

The output doesn’t print the SeeTable()

I mean, is there anything in TurnOrder?

I added 3 values into TurnOrder and when I triggered the function it printed them.

local TurnOrder = {
	[1] = "Isaque",
	[2] = "Hey",
	[3] = "Hoi!"
}

function SeeTable()
	for i,v in pairs(TurnOrder) do
		print(i,v)
	end
end

SeeTable()

Screenshot_4

Here is the script:

local CurrentTurn = 1

function AddPlayers()
	for i,name in pairs(game.Players:GetPlayers()) do
		TurnOrder[i] = name
	end
end

function StartTurn()
	if not TurnOrder[CurrentTurn] then 
		CurrentTurn = 1 
	end
	local Player = TurnOrder[CurrentTurn]
	wait(4)

	--Do something to the player here!

CurrentTurn += 1
StartTurn() -- Starts the turn again!
end

function SeeTable()
	for i,v in pairs(TurnOrder) do
		print(i,v)
	end
end

SeeTable()
AddPlayers() -- Add players to TurnOrder
StartTurn() -- Start the game!

As you’re using the script completly raw, we are not getting the results you would get in your game normally.

You’re not seeing anything because the players have not loaded in the game, so when the AddPlayers() function executes It’ll not get anything, the TurnOrder will be completly empty. You’d need to wait until there’s players in-game

That whole script is just the idea of the player turns for the board game, but you’d of course need to make all the gameplay suit your script, and it would work the way it needs to.

2 Likes

This is something you can use to efficiently track turns, as well as ending a turn.

local Players = {} -- keep a list of all the players
local Turn = 1 -- First player turn

function GetPlayerTurn() -- Gets the player whose turn it is.
   return Players[Turn]
end

function EndTurn() -- Ends current player's turn, and returns the next player.
  Turn = Turn + 1
  if Turn > #Players then
     Turn = 1
  end
  return GetPlayerTurn()
end

Make sure you add each player in the game inside the Players table. You can do so by using the following command:

table.insert(Players, PlayerObject)

or if you wanna include all the players:

for i,v in pairs (game.Players:GetPlayers()) do
    table.insert(Players, v)
end
2 Likes