Assign player to a role via table

Sorry in advanced if am not explaining it good enough as i don’t know how to!

So basically am trying to create 4 roles (Ill add more in the future) via tables and not with team service. So when the game start you will get 1 out of 4 roles randomly. with different characters and abilities. But the thing is. I have no idea how to do that

local players = game.Players

local Roles = { -- the roles table containing the roles
	"Class d",
	"Brute Class d",
	"Low ranking Researcher",
	"High ranking Researcher",
}

players.PlayerAdded:Connect(function() -- everytime a player is added they will get a random roll (Will change later on)

end)
local Random=math.random(1,#Roles)

players.PlayerAdded:Connect(function(player)
--Get a folder with all the role related character models 
--(i'm not accounting for several characters of one role btw)

local newChar = roleCharacters:FindFirstChild(Roles[Random]):Clone()
newChar.Parent=workspace --or make a folder for player characters if you want
newChar.Name=player.Name --Just to keep it clean
player.Character = newChar

--if you want to put them in a specific spawn point
newChar.Position=workspace.SpawnPoints:FindFirstChild(Roles[Random])
--if you want them all to spawn in the spawnpoint
newChar.Position=workspace.SpawnLocation.Position + Vector3.new(0,3,0)
--The vector is an offset, they will spawn on the ground otherwise
end)

For role specific abilities it’s more complicated, you’re gonna have to get the role from a LocalScript and then use ContextActionService to bind the controls, and/or make Gui buttons for them (you have to for mobile, they can be an alternative for PC)

I added it but nothing is happing

Code:

local players = game.Players

local Roles = {
	"Class-D",
	"Brute Class-D",
	"Low ranking Researcher",
	"High ranking Researcher",
}

local Randomness = math.random(1,#Roles)

players.PlayerAdded:Connect(function(player)
	local roleCharacters = game.ReplicatedStorage.RoleRelated

	local newChar = roleCharacters:FindFirstChild(Roles[Randomness]):Clone()
	newChar.Parent = workspace
	newChar.Name = player.Name
	player.Character = newChar
	print(newChar.Name)
end)

The “roleCharacters” is linked the replicated storage with just some rig models with clothing and accessories in the folder “RoleRelated” named after the table. am not getting any error and have tried adding a wait. may i be becuase the script is in serverscriptservice and is a server script?

If the player spawns as their default player go to Players in the explorer and disable CharacterAutoLoads

ServerScriptService is designed exactly for server scripts so it shouldn’t cause problems

Something else i forgot to mention, because it’s for later, you also have to account for when the player is killed

--Make the spawning off the player a function you can call instead of an anonymous function
newChar.Humanoid.Died:Once(function()
newChar:Destroy()
end)

newChar.Destryoing:Once(function()
-- you call the function here to make them respawn
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.