Hey, I want to make a script similar to Murder Mystery 2 where players get a role, and then are teleported to a random spawn point. This is what I was thinking of doing,
-
Players are added to 2 tables one called plrs and another called roles
-
Clones the map and parents it to the workspace
-
A function is called, the function has a repeat unit loop that picks a random player in a table then removes them from the table and gives them a role + weapon. The loop ends when there are no more players in the table, roles.
-
There’s a folder in the map model that contains all of the spawn points, so they are looped through and added to a table called AvailableSpawnPoints.
-
In a repeat until loop a random player is chosen from the plrs table, then removed from the table. They are then teleported to a spawn point in AvailableSpawnPoints and that spawn point is removed from the table. (Should the spawn point be a part or an actual SpawnLocation?)
What do you think of this script? (code does not work)
Thanks!
Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Arena = ServerStorage:WaitForChild("Bang")
local plots = ServerStorage:WaitForChild("BangPlots"):GetChildren()
while true do
local full = false
local waiting = {}
local plrs = {}
local roles = {}
game.ReplicatedStorage.AddPlayerToTableBang.OnServerEvent:Connect(function(player)
if full == true then
table.insert(waiting,player)
elseif #plrs >= 1 then
for i, v in pairs(plrs) do
if not table.find(plrs, player) then
table.insert(plrs,player)
table.insert(roles,player)
end
end
else
table.insert(plrs,player)
table.insert(roles,player)
end
end)
game.ReplicatedStorage.ExitTableBang.OnServerEvent:Connect(function(plr)
for i, player in pairs(plrs)do
if player == plr then
if player then
table.remove(plrs, i)
table.remove(roles,i)
local name = player.Name
local Playercharacter = game.Workspace:FindFirstChild(name)
local character = Playercharacter.HumanoidRootPart
character.CFrame = game.Workspace.SpawnLocation.CFrame
end
end
end
end)
if full == false then
for i, player in pairs(waiting) do
table.insert(plrs,player)
table.insert(roles,player)
table.remove(waiting,player)
end
end
repeat
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
else
table.remove(plrs,i)
table.remove(roles,i)
end
end
end
for i, player in pairs(plrs) do
player.playerAmount.Value = #plrs
end
wait(1) until #plrs == 5 and #roles == 5
print("there are 5 players")
full = true
for i, plot in ipairs(plots) do
if plot.Bool.Value == false then
position = plot.CFrame
plot.Bool.Value = true
break
end
end
local ClonedArena = Arena:Clone()
ClonedArena.Parent = workspace
ClonedArena.PrimaryPart = ClonedArena.ArenaGround
ClonedArena:SetPrimaryPartCFrame(position)
print("cloned the arent")
local function pickRoles()
local number = 1
repeat
print(#roles)
local randomizedPlayer = roles[math.random(1,#roles)]
for x, plr in pairs(roles) do
if plr == randomizedPlayer then
table.remove(roles,x)
end
end
if number == 1 then
local name = randomizedPlayer.Name
game.Players:FindFirstChild(name).Sheriff.Value = true
game.Players:FindFirstChild(name).Lives.Value = 3
ReplicatedStorage.Sheriff:FireClient(randomizedPlayer)
local knife = ServerStorage:FindFirstChild("Knife"):Clone()
knife.Parent = randomizedPlayer.Backpack
workspace:FindFirstChild(name).Head.BillboardSheriff.Enabled = true
number = number + 1
elseif number == 2 then
local name = randomizedPlayer.Name
game.Players:FindFirstChild(name).Deputy.Value = true
game.Players:FindFirstChild(name).Lives.Value = 2
ReplicatedStorage.Deputy:FireClient(randomizedPlayer)
local knife = ServerStorage:FindFirstChild("Knife"):Clone()
knife.Parent = randomizedPlayer.Backpack
number = number + 1
elseif number == 3 then
local name = randomizedPlayer.Name
game.Players:FindFirstChild(name).Renegade.Value = true
game.Players:FindFirstChild(name).Lives.Value = 2
ReplicatedStorage.Renegade:FireClient(randomizedPlayer)
local knife = ServerStorage:FindFirstChild("Knife"):Clone()
knife.Parent = randomizedPlayer.Backpack
number = number + 1
elseif number == 4 then
local name = randomizedPlayer.Name
game.Players:FindFirstChild(name).Outlaw.Value = true
game.Players:FindFirstChild(name).Lives.Value = 2
ReplicatedStorage.Outlaw:FireClient(randomizedPlayer)
local knife = ServerStorage:FindFirstChild("Knife"):Clone()
knife.Parent = randomizedPlayer.Backpack
number = number + 1
elseif number == 5 then
local name = randomizedPlayer.Name
game.Players:FindFirstChild(name).Outlaw.Value = true
game.Players:FindFirstChild(name).Lives.Value = 2
ReplicatedStorage.Outlaw:FireClient(randomizedPlayer)
local knife = ServerStorage:FindFirstChild("Knife"):Clone()
knife.Parent = randomizedPlayer.Backpack
number = number + 1
end
until #roles == 0
end
pickRoles()
print("pick roles succeeded")
local SpawnPoints = ClonedArena:FindFirstChild("SpawnPoints")
local AvailableSpawnPoints = {}
for i, spawns in pairs(SpawnPoints:GetChildren()) do
table.insert(AvailableSpawnPoints, spawns)
end
repeat
local chosen = math.random(1, #plrs)
local player = plrs[chosen]
table.remove(plrs, chosen)
print(#plrs)
if player then
character = player.Character
if character then
print(#AvailableSpawnPoints)
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
print(AvailableSpawnPoints[1].Name)
table.remove(AvailableSpawnPoints,1)
player.playerAmount.Value = 0
game.ReplicatedStorage.WaitScreenEndBang:FireClient(player)
end
end
until #plrs == 0
full = false
end