THANK YOU SO MUCH, it finally works if I create a script based on your code. but if I also want to apply it to a module script (which I didn’t create, it’s from Alvinblox), it doesn’t work. I’m trying to apply your code to both scripts because I want to use some Alvinblox script functions and basically build on that. its “killer” is the “piggy”, so i converted the part where your code says “killer” to “piggy”, but it doesn’t work. here is the whole module script, can you help me find the error and fix it? Thanks in advance :))
local module = {}
local status = game.ReplicatedStorage.Status
--THIS IS A PART OF YOUR CODE!!!
-- First, declare the Players service. This will save you a lot of work.
local Players = game:GetService("Players")
-- Then, if you'd like, the tables.
local players = {}
--[[
We just need this table to store the players in the game, as well as their chances.
Each player will have a chance to be the Piggy, depending on how long they've been in the game,
and if they have been the Piggy before.
]]
-- Now let's get into the functions.
-- First, we want to create a function to assign the players to the table.
function assignPlayersChance(players)
for index, player in pairs(players) do
table.insert(players, player.Name)
players[index] = 1 -- initialize the chances of the players
end
end
--END OF YOUR CODE!!!
function module.Intermission(length)
for i = length,0,-1 do
status.Value = "Intermission - "..i..""
wait(1)
end
end
function module.SelectChapter() --chosing map
-- i will code here
end
function module.ChoosePiggy(players)
--THIS IS A PART OF YOUR CODE!!!
--[[
Here, we want to choose the Piggy.
Once chosen, we'll assign their chance to 0.
This is so they can't be the Piggy again.
Then, we'll increase the chances of the other players.
]]
local function rouletteSelection(player, chances)
--[[
We'll use a task.spawn to run this function as we call it.
Now, this function will take two arguments: player and chances.
This should be obvious, but I'll explain it anyway.
Within this function, we want to choose a random player from the table.
However: we want to choose a player that has a higher chance of being the Piggy based on their chances.
For that, we'll use the Roulette Wheel Selection algorithm.
]]
local total: number = 0
for key, chance in pairs(players) do
total += chance -- add the chances of the players to the total
end
local random = math.random() -- get a random number between 0 and 1
local runningTotal: number = 0
local chosenPiggy: string -- initialize the chosenPiggy variable as a string.
-- this string is local, and will be returned at the end of the function.
for player, chance in pairs(players) do
local probability = chance / total -- get the probability of the player
runningTotal = runningTotal + probability -- add the probability to the runningTotal
if runningTotal > random then -- check if the runningTotal is greater than the random number
chosenPiggy = player -- if it is, set the chosenPiggy to the player
players[player] = 0 -- set the Piggy's chance to 0
break -- break the loop
end
end
return chosenPiggy
end
local Piggy: string = rouletteSelection() -- initialize the Piggy variable as a string
for key, chance in pairs(players) do
players[key] += 1 -- increase the chances of the players, change this to your liking.
if table.find(players, Piggy) then -- check if the Piggy is in the table
players[key] = 0 -- if they are, set their chance to 0
end
end
return Piggy -- return the string, so "Piggy = assignPiggy(players)" will work.
end
--END OF YOUR CODE!!!
function module.DressPiggy(piggy)
--this is to convert the killer in their selected skin, don't worry about it
end
function module.TeleportPiggy(player)
--i will code here
end
function module.TeleportPlayers(players, mapSpawns)
--another part not to worry about
end
function module.InsertTag(contestants,tagName) --this is to find the players
for i, player in pairs(contestants) do
local Tag = Instance.new("StringValue")
Tag.Name = tagName
Tag.Parent = player
end
end
local function toMS(s)
return ("%02i:%02i"):format(s/60%60, s%60)
end
function module.StartRound(length,piggy,chapterMap)
local outcome
game.ServerStorage.GameValues.GameInProgress.Value = true
for i = length,0,-1 do
if i == (length - 20) then
--the killer will spawn at this point, but i know how to do that
end
local contestants = {}
local isPiggyHere = false
local Escapees = 0
for i, player in pairs(game.Players:GetPlayers()) do
if player:FindFirstChild("Contestant") then
table.insert(contestants,player)
elseif player:FindFirstChild("Piggy") then
isPiggyHere = true
end
if player:FindFirstChild("Escaped") then
Escapees = Escapees + 1
end
end
status.Value = toMS(i)
if Escapees > 0 then
outcome = "escaped"
break
end
if not isPiggyHere then
outcome = "piggy-left"
break
end
if #contestants == 0 then
outcome = "piggy-killed-everyone"
break
end
if i == 0 then
outcome = "time-up"
break
end
wait(1)
end
if outcome == "piggy-killed-everyone" then
--i will code here
elseif outcome == "time-up" then
--same thing here
elseif outcome == "piggy-left" then
--same
elseif outcome == "escaped" then
--and once again
end
wait(5)
end
function module.RemoveTags()
--this is where the round starts again, i know what to do ;)
end
--THIS IS A PART OF YOUR CODE!!!
Players.PlayerAdded:Connect(function(player)
table.insert(players, player.Name) -- insert the player into the table
players[player.Name] = 0 -- initialize the chances of the player
end)
Players.PlayerRemoving:Connect(function(player)
for index, player in pairs(players) do -- iterate through the table
if table.find(players, player.Name) then -- check if the player is in the table
table.remove(players, table.find(players, player.Name)) -- if they are, remove them from the table
end
end
end)
--END OF YOUR CODE!!!
return module
Edit: also, is there any way to make a gui that shows each player their percentage to be the killer/piggy?