Unexpected Behavior
I am trying to assign players to slots in my state, this is what my state looks like
When I iterate over the players (in my test case only 1), the same player is assigned to EVERY slot.
//this.lanes
{
team1: {
slot1: {
stat1: number
stat2: number
player?: Player
},
slot2: {
etc...
}
etc...
}
team2: {
etc...
}
}
//this.lanes
{
team1: {
slot1: {
stat1: number
stat2: number
player?: Player
},
slot2: {
etc...
}
etc...
}
team2: {
etc...
}
}
setupPlayers() {
const players = shuffle(PlayerService.GetPlayers())
players.forEach((player, index) => {
this.assignPlayerToTeam(player, index)
})
}
setupPlayers() {
const players = shuffle(PlayerService.GetPlayers())
players.forEach((player, index) => {
this.assignPlayerToTeam(player, index)
})
}
assignPlayerToTeam(player: Player, index: number) {
let side: keyof typeof this.lanes = "team1"
if (index <= 3) side = 'team2'
if (index === 0) this.lanes[side].slot1.player = player
else if (index === 1) this.lanes[side].slot2.player = player
else if (index === 2) this.lanes[side].slot3.player = player
else if (index === 3) this.lanes[side].slot4.player = player
}
assignPlayerToTeam(player: Player, index: number) {
let side: keyof typeof this.lanes = "team1"
if (index <= 3) side = 'team2'
if (index === 0) this.lanes[side].slot1.player = player
else if (index === 1) this.lanes[side].slot2.player = player
else if (index === 2) this.lanes[side].slot3.player = player
else if (index === 3) this.lanes[side].slot4.player = player
}
3 Replies
Why does it assign someone to every slot if it only iterates ONCE. In my brain, this code doesn't allow for any of the other slots to be filled if there's only 1 player.
only 1 slot should be filled
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
This is what
shuffle only changes the order of the array, I have verified that the size of the array is unchanged.
Yes I'm sorry
I'm just going to dump it
Here are the types
oh my gosh...
Like I break out the whole loop into a function and stop using methods?
or just the clone
Bruh thank you so much! I was so dumbfounded by this behavior...
this.lanes[side]
looks like
//this.lanes
const lanes = {
team1: {...},
team2: {...}
}
//this.lanes[side] (side === keyof typeof lanes)
const side = {slot1: {...}, slot2: {...}, slot3: {...}, slot4: {...}}
//this.lanes
const lanes = {
team1: {...},
team2: {...}
}
//this.lanes[side] (side === keyof typeof lanes)
const side = {slot1: {...}, slot2: {...}, slot3: {...}, slot4: {...}}
export function shuffle<T>(array: Array<T>) {
let currentIndex = array.size(), randomIndex;
// While there remain elements to shuffle.
while (currentIndex !== 0) {
// Pick a remaining element.
randomIndex = math.floor(math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
export function shuffle<T>(array: Array<T>) {
let currentIndex = array.size(), randomIndex;
// While there remain elements to shuffle.
while (currentIndex !== 0) {
// Pick a remaining element.
randomIndex = math.floor(math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
import { Components } from "@flamework/components";
import {Dependency, OnStart, Service } from "@flamework/core";
import { AIController } from "server/components/AI/AIController";
import { shuffle } from "server/utility";
import { CollisionManager } from "../CollisionManager";
import { initTeamLanes, TeamLanes } from "./Game.types.";
const components = Dependency<Components>();
const PlayerService = game.GetService("Players")
const ServerStorage = game.GetService("ServerStorage")
const Dummy = ServerStorage.Creeps.Dummy
const TEMPWAYPOINTS = game.Workspace.Map.Waypoints
const MIN_PLAYERS = 1
/** Game encompasses the main loop
*
* Loop:
* 1) Check if there are enough players to start the game, otherwise wait for more players
* 2) If there are enough players, start the game
* 3) Assign players to teams/lanes
* 4) Spawn players
* 6) wait 45 seconds for players to setup their first units
* 7) start the wave loop
* 8) wait for win condition
* 9) end game
*
*/
@Service()
export class Game implements OnStart {
constructor(private collisionManager: CollisionManager) {}
private gameStarted = false
private playerCount = 0
private lanes: TeamLanes = initTeamLanes
onStart() {
PlayerService.PlayerAdded.Connect(() => {
this.playerCount++
if (!this.gameStarted && this.playerCount >= MIN_PLAYERS) {
this.gameStarted = true
this.startGame()
}
})
PlayerService.PlayerRemoving.Connect(() => {
this.playerCount--
})
}
startGame() {
//* testing ai spawn and movement
// while (true) {
// wait(1)
// const ai = Dummy.Clone()
// this.collisionManager.addAI(ai)
// ai.Parent = game.Workspace
// const aiController = components.getComponent<AIController>(ai)
// if (aiController) {
// const waypoints = TEMPWAYPOINTS.map(waypoint => waypoint.Position)
// aiController.waypointList = waypoints
// } else {
// warn("AI controller not found")
// }
// }
this.waitForPlayers(MIN_PLAYERS)
this.setupPlayers()
}
waitForPlayers(minPlayers: number) {
while (this.playerCount < minPlayers) {
wait(1)
}
}
setupPlayers() { //! having trouble with this function.... same player set for all lanes... not teleporting
const players = shuffle(PlayerService.GetPlayers())
players.forEach((player, index) => {
this.assignPlayerToTeam(player, index)
})
//teleport players to spawn points
for (const [, v] of pairs(this.lanes)) {
for (const [lane, v2] of pairs(v)) {
const player = v2.player
if (player) {
const character = player.Character || player.CharacterAdded.Wait()[0]
const primaryPart = character.PrimaryPart
print("preparing player " + character.Name)
if (primaryPart) primaryPart.CFrame = new CFrame(game.Workspace.Map.PlayerAreas[lane].Position)
print("teleported")
}
}
}
}
assignPlayerToTeam(player: Player, index: number) {
let side: keyof typeof this.lanes = "east"
if (index <= 3) side = 'west' //first 4 players are on the west side
if (index === 0) this.lanes[side].NorthWest.player = player
else if (index === 1) this.lanes[side].SouthWest.player = player
else if (index === 2) this.lanes[side].NorthEast.player = player
else if (index === 3) this.lanes[side].SouthEast.player = player
}
}
import { Components } from "@flamework/components";
import {Dependency, OnStart, Service } from "@flamework/core";
import { AIController } from "server/components/AI/AIController";
import { shuffle } from "server/utility";
import { CollisionManager } from "../CollisionManager";
import { initTeamLanes, TeamLanes } from "./Game.types.";
const components = Dependency<Components>();
const PlayerService = game.GetService("Players")
const ServerStorage = game.GetService("ServerStorage")
const Dummy = ServerStorage.Creeps.Dummy
const TEMPWAYPOINTS = game.Workspace.Map.Waypoints
const MIN_PLAYERS = 1
/** Game encompasses the main loop
*
* Loop:
* 1) Check if there are enough players to start the game, otherwise wait for more players
* 2) If there are enough players, start the game
* 3) Assign players to teams/lanes
* 4) Spawn players
* 6) wait 45 seconds for players to setup their first units
* 7) start the wave loop
* 8) wait for win condition
* 9) end game
*
*/
@Service()
export class Game implements OnStart {
constructor(private collisionManager: CollisionManager) {}
private gameStarted = false
private playerCount = 0
private lanes: TeamLanes = initTeamLanes
onStart() {
PlayerService.PlayerAdded.Connect(() => {
this.playerCount++
if (!this.gameStarted && this.playerCount >= MIN_PLAYERS) {
this.gameStarted = true
this.startGame()
}
})
PlayerService.PlayerRemoving.Connect(() => {
this.playerCount--
})
}
startGame() {
//* testing ai spawn and movement
// while (true) {
// wait(1)
// const ai = Dummy.Clone()
// this.collisionManager.addAI(ai)
// ai.Parent = game.Workspace
// const aiController = components.getComponent<AIController>(ai)
// if (aiController) {
// const waypoints = TEMPWAYPOINTS.map(waypoint => waypoint.Position)
// aiController.waypointList = waypoints
// } else {
// warn("AI controller not found")
// }
// }
this.waitForPlayers(MIN_PLAYERS)
this.setupPlayers()
}
waitForPlayers(minPlayers: number) {
while (this.playerCount < minPlayers) {
wait(1)
}
}
setupPlayers() { //! having trouble with this function.... same player set for all lanes... not teleporting
const players = shuffle(PlayerService.GetPlayers())
players.forEach((player, index) => {
this.assignPlayerToTeam(player, index)
})
//teleport players to spawn points
for (const [, v] of pairs(this.lanes)) {
for (const [lane, v2] of pairs(v)) {
const player = v2.player
if (player) {
const character = player.Character || player.CharacterAdded.Wait()[0]
const primaryPart = character.PrimaryPart
print("preparing player " + character.Name)
if (primaryPart) primaryPart.CFrame = new CFrame(game.Workspace.Map.PlayerAreas[lane].Position)
print("teleported")
}
}
}
}
assignPlayerToTeam(player: Player, index: number) {
let side: keyof typeof this.lanes = "east"
if (index <= 3) side = 'west' //first 4 players are on the west side
if (index === 0) this.lanes[side].NorthWest.player = player
else if (index === 1) this.lanes[side].SouthWest.player = player
else if (index === 2) this.lanes[side].NorthEast.player = player
else if (index === 3) this.lanes[side].SouthEast.player = player
}
}
export type TeamLanes = {east: Lanes, west: Lanes}
export type Lanes = {NorthWest: LaneInfo, NorthEast: LaneInfo, SouthWest: LaneInfo, SouthEast: LaneInfo}
export type LaneInfo = {player?: Player, gold: number, lumber: number}
export const initLaneInfo = {gold: 0, lumber: 0}
export const initLanes: Lanes = {NorthEast: initLaneInfo, NorthWest: initLaneInfo, SouthEast: initLaneInfo, SouthWest: initLaneInfo}
export const initTeamLanes: TeamLanes = {east: initLanes, west: initLanes}
export type TeamLanes = {east: Lanes, west: Lanes}
export type Lanes = {NorthWest: LaneInfo, NorthEast: LaneInfo, SouthWest: LaneInfo, SouthEast: LaneInfo}
export type LaneInfo = {player?: Player, gold: number, lumber: number}
export const initLaneInfo = {gold: 0, lumber: 0}
export const initLanes: Lanes = {NorthEast: initLaneInfo, NorthWest: initLaneInfo, SouthEast: initLaneInfo, SouthWest: initLaneInfo}
export const initTeamLanes: TeamLanes = {east: initLanes, west: initLanes}