edit: in short for the script, it picks a disaster from a table
So I have this old disaster kit and i've been trying to figure out how to add weighted chance to disasters. So basically for each round a disaster isn't picked, the chance would go up, and when it's finally picked, the chance would go back down to default. How would I achieve this?
--[[
DISASTER KIT V2 ~kit created by Vyriss (LAST UPDATED ON APRIL 28, 2019)
What's new:
- Cleaner and better code, rewritten most of everything
- New gears (Gravity Coil, Black Ninja Stars and Epic Katana)
- Refined Noobs disaster
- Bloxxing Noobs give extra points
- New Flash Flood and Acid Rain disaster(s)
- Unique coins and duration configuration for each disaster
- Using updated methods such as inserting objects in ServerStorage and ServerScriptService
- DataStores! This kit now allows you to save your data. (Points and Survivals)
- Regening buildings!
BEFORE YOU DO ANYTHING, this model MUST be repositioned.
At the bottom of Roblox Studio you will see a bar that says "Run a command", run this exact code under this line without quotes:
"workspace["Open this Model and read Game"]:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, .2, 0)))"
This will ensure that all disasters spawn at the correct position of the level.
If done, ungroup this model.
Read commented lines carefully. This should tell you how to modify and add your own disasters and place the needed items to the right place.
Move "Gears", "Disasters", "Clones", and "DataStore" to ServerStorage in Explorer
Move this script ("Game") to ServerScriptService in Explorer
Leave everything else in Workspace
Look inside models and mess around with configurations such as the shop buttons to change the cost
--]]
-- // ========================================================================================================================================================== \\ --
disasters = {
{"Noobs", Points = 250, Duration = 25},
{"Flash Flood", Points = 200, Duration = 18},
{"Criminal Sentai Redman", Points = 150, Duration = 20},
{"The Floor is Lava", Points = 175, Duration = 12},
{"Tako Rain", Points = 100, Duration = 15},
{"Bomb Rain", Points = 150, Duration = 18},
{"Prop Rain", Points = 200, Duration = 31},
{"Enemy Helicopter", Points = 300, Duration = 30},
{"Rising Acid", Points = 300, Duration = 20},
{"Meteor Shower", Points = 300, Duration = 30},
{"Zombies", Points = 300, Duration = 35},
{"Giant Lava Spinner", Points = 300, Duration = 20},
{"Vacuum Noob", Points = 400, Duration = 25},
{"Bunny Girls", Points = 400, Duration = 25},
{"Nextbot", Points = 400, Duration = 30},
{"Corporate Entity", Points = 400, Duration = 30},
{"Don't Move", Points = 150, Duration = 10},
--{"Don't Press the Button", Points = 200, Duration = 30},
--{"Earthquake", Points = 100, Duration = 16},
{"Delinquents", Points = 400, Duration = 30},
{"Kawaii Grandpas", Points = 400, Duration = 30},
{"Imposter", Points = 200, Duration = 30},
{"Rider Kick", Points = 200, Duration = 30},
{"Floating Away", Points = 200, Duration = 25},
{"Piggy", Points = 250, Duration = 25},
{"One Punch Man", Points = 400, Duration = 30},
{"Hole in the Wall", Points = 400, Duration = 45},
{"Shoop da Woop", Points = 400, Duration = 30},
--{"Get Down", Points = 400, Duration = 30},
--{"The Man Behind the Slaughter", Points = 300, Duration = 35},
}
-- The table above is a reference to hold information about a disaster.
-- The table defines the name of the disaster, the points it gives after you survive, and the duration of how long the disaster will be active for the round
-- If you want to make a new disaster, simply copy a table to a new line and configure it however you'd like. Example:
-- disasters = {
-- {"Noob", Points = 200, Duration = 30},
-- {"Zombie", Points = 240, Duration = 30},
-- {"Flash Flood", Points = 300, Duration = 20},
-- {"Acid Rain", Points = 200, Duration = 20},
-- {"Tornado", Points = 500, Duration = 45},
-- {"Disaster 6", Points = 1000,Duration = 60},
-- }
-- Disaster names ARE case-sensitive. Your disaster models should have the exact names inside the tables.
-- Your disaster models MUST be inserted to ServerStorage.
breakTime = 10 -- The amount of time to wait between each disaster.
roundsToRegenStructures = 7 -- How many rounds until the structures regen. Setting this to 1 would make the buildings respawn after every round
countdownMessage = "The next disaster will occur in %s seconds." -- The message displayed between disasters. %s will be replaced with the number of seconds left.
disasterMessage = "Disaster: %s" -- The message displayed when a disaster occurs. %s will be replaced with the disaster name. Set to nil if you do not want a message
lastDisaster = nil
-- // ========================================================================================================================================================== \\ --
-- Leave the code alone below unless you know what you're doing
local dataStore = require(game.ServerStorage.DataStore)
local survivors = {}
local currentDisaster = nil
local timeToCount = 0
local rounds = 0
local states = Instance.new("Folder")
states.Name = "PlayerStates"
function regenStructures()
workspace.Structures:ClearAllChildren()
local maps = game.ServerStorage.Maps:GetChildren()
local chosenmap = maps[math.random(1, #maps)]
local mapCopy = chosenmap:Clone()
mapCopy.Parent = workspace.Structures
mapCopy:MakeJoints()
end
function compilePlayers()
local names = ""
if #survivors == 1 then return survivors[1] end
for i = 1, #survivors do
if i == #survivors then
names = names.. "and ".. survivors[i]
else
names = names.. survivors[i] .. ", "
end
end
return names
end
function died(playerName, humanoid)
if humanoid and humanoid.Health == 0 then
for i, v in pairs(survivors) do
if v == playerName then
local dead = Instance.new("BoolValue")
dead.Name = "Died"
dead.Parent = humanoid.Parent
table.remove(survivors, i)
end
end
end
end
function destroyMessage()
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Message") then
v:Destroy()
end
end
end
function createOrUpdateMessage(msg)
if workspace:FindFirstChild("_Message") then
workspace["_Message"].Text = msg
else
local message = Instance.new("Message")
message.Name = "_Message"
message.Text = msg
message.Parent = game.Workspace
end
end
function destroyHint()
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Hint") then
v:Destroy()
end
end
end
function createOrUpdateHint(msg)
if workspace:FindFirstChild("_Hint") then
workspace["_Hint"].Text = msg
else
local message = Instance.new("Hint")
message.Name = "_Hint"
message.Text = msg
message.Parent = game.Workspace
end
end
function countDown(cdt, msg)
for i = 1, cdt do
createOrUpdateHint(msg .. cdt - i + 1)
wait(1)
end
end
function _G.updateLeaderboard()
for i, v in pairs(game.Players:GetChildren()) do
local leaderstats = v:FindFirstChild("leaderstats")
if leaderstats then
local fakePoints = leaderstats:FindFirstChild("Points")
local fakeSurvivals = leaderstats:FindFirstChild("Survivals")
fakePoints.Value = dataStore:GetStat(v, "Points") or 0
fakeSurvivals.Value = dataStore:GetStat(v, "Survivals") or 0
end
end
end
function newPlayer(player)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local pts = Instance.new("IntValue")
pts.Name = "Points"
pts.Parent = leaderstats
local survs = Instance.new("IntValue")
survs.Name = "Survivals"
survs.Parent = leaderstats
wait(3) -- wait a bit before fetching player data when the player enter the game
pts.Value = dataStore:GetStat(player, "Points") or 0
survs.Value = dataStore:GetStat(player, "Survivals") or 0
end
game.Players.ChildAdded:connect(newPlayer)
regenStructures()
while true do
-- math.randomseed(os.clock)
if rounds == roundsToRegenStructures then
regenStructures()
rounds = 0
end
rounds = rounds + 1
countDown(breakTime, "Next disaster will spawn in ")
_G.updateLeaderboard()
destroyHint()
local connections = {}
survivors = {}
for i, v in pairs(game.Players:GetChildren()) do
if v.Character and v.Character:FindFirstChild("Humanoid") then
table.insert(survivors, v.Name)
table.insert(connections,
v.Character.Humanoid.HealthChanged:connect(function()
died(v.Name, v.Character.Humanoid)
if v:FindFirstChild("Died") == true then
v.Died:Destroy()
end
end)
)
end
end
print(lastDisaster)
currentDisaster = disasters[math.random(#disasters)]
if currentDisaster.Name == lastDisaster then
currentDisaster = disasters[math.random(#disasters)]
end
if game.ServerStorage.Disasters:FindFirstChild(currentDisaster[1]) then -- disaster exists, proceed to spawn it
createOrUpdateMessage("Disaster: " .. currentDisaster[1])
wait(3)
destroyMessage()
--
local disasaterCopy = game.ServerStorage.Disasters[currentDisaster[1]]:clone()
disasaterCopy.Parent = game.Workspace
lastDisaster = disasaterCopy.Name
disasaterCopy:makeJoints()
countDown(currentDisaster.Duration, "Current Disaster: " .. currentDisaster[1] .. ", ending in ")
destroyHint()
disasaterCopy:Destroy()
--
if #survivors ~= 0 then
createOrUpdateMessage(compilePlayers() .. " survived and won " .. currentDisaster.Points .. " points!")
else
createOrUpdateMessage("Nobody survived!")
end
for i, v in pairs(survivors) do
local player = game.Players:FindFirstChild(v)
if player then
dataStore:AddStat(player, "Points", currentDisaster.Points) -- award survivors points
dataStore:AddStat(player, "Survivals", 1) -- award survivors 1 survival
end
end
_G.updateLeaderboard()
wait(3)
destroyMessage()
else
-- warn dev
createOrUpdateHint('ERROR: The game has failed to locate "' .. currentDisaster[1] .. '" in the Disasters folder in ServerStorage.')
while true do
wait(1)
end
end
end