I have this old script from 2016 that I created. At the time I was trying to create a game with it, but now that I know a ton more about scripting than I used to I was wondering if there is anyway I could organize this script better, It was one of the first long scripts I've ever written.
Ive built a game off this script and there are certain parts of the script I do not need anymore and I want to remove them so the server doesnt have to worry about those lines of code but when I remove some it breaks the rest, I've been trying to clean it up without breaking it.
But here it is, also maybe you could give me some advice on where I could improve.
--Made by Kofikingston9000
local roundtime = 60 * 4 local intermissiontime = 20
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData") local serverstorage = game:GetService("ServerStorage") local replicatedstorage = game:GetService("ReplicatedStorage") local event = replicatedstorage:WaitForChild("RemoteEvent") local maps = serverstorage:WaitForChild("Maps") local mapholder = game.Workspace:WaitForChild("MapHolder") local statustag = replicatedstorage:WaitForChild("StatusTag") local timertag = replicatedstorage:WaitForChild("TimerTag")
local playerdata = {}
--set up score leaderboard --keep track of points --Drop sheriff net gun if bloxxed --make sure sheriff doesn't blox bystandars
function permanentsave(player) if player then local mydata = playerdata[player.userId] if mydata then datastore:SetAsync(player.userId, mydata) end end end
game.Players.PlayerRemoving:connect(function(player) if player then permanentsave(player) playerdata[player.userId] = nil end end)
game.OnClose = function() for i, v in pairs(playerdata) do datastore:SetAsync(i, v) end end
function findplayerbyid(userid) for _, player in pairs(game.Players:GetPlayers()) do if player and player.userId == userid then return player end end end
function newcharacter(bloxxedplayer, character) local humanoid = character:FindFirstChild("Humanoid") if humanoid then humanoid.Died:connect(function() end) end end
-- Officer and agent no kill good guys function newcharacter(bloxxedplayer, character) local humanoid = character:FindFirstChild("Humanoid") if humanoid then humanoid.Died:connect(function()
local tag = humanoid:FindFirstChild("creator")
if tag and tag.Value and tag.Value.Parent == game.Players then
if tag.Value ~= culprit and bloxxedplayer ~= culprit then
local wrongdoer = tag.Value
if wrongdoer.Character and wrongdoer.Character.Parent then
local wrongdoerhumanoid = wrongdoer.Character:FindFirstChild("Humanoid")
if wrongdoerhumanoid then
wrongdoerhumanoid.Health = 0
end
end
end
end
end)
end
end
while true do --load a random map statustag.Value = "Loading Map" timertag.Value = -1 mapholder:ClearAllChildren() wait(2) local allmaps = maps:GetChildren() local newmap = allmaps[math.random(1, #allmaps)]:clone() newmap.Parent = mapholder wait(2)
--wait for contestants
while true do
wait(4)
contestants = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid and humanoid.Health > 0 then
table.insert(contestants, player)
end
end
end
if #contestants >= 3 then
wait(5)
break
else
statustag.Value = "Waiting for Players"
timertag.Value = -1
end
end
statustag.Value = "Match Time"
timertag.Value = roundtime
-- Choose culprit
culprit = contestants[math.random(1, #contestants)]
--choose sheriff
while true do
randomplayer = contestants[math.random(1, #contestants)]
if randomplayer ~= culprit then
officer = randomplayer
break
end
end
-- Choose the Agent
while true do
randomplayer = contestants[math.random(1, #contestants)]
if randomplayer ~= culprit and officer then
agent = randomplayer
break
end
end
--teleport contestants
local spawnsmodel = newmap:WaitForChild("Spawns")
local spawns = spawnsmodel:GetChildren()
for _, player in pairs(contestants) do
if player and player.Character and #spawns > 0 then
local torso = player.Character:WaitForChild("Torso")
local humanoid = player.Character:WaitForChild("Humanoid")
local spawnindex = math.random(1, #spawns)
local spawn = spawns[spawnindex]
if spawn and torso and humanoid then
humanoid.Health = 100
humanoid.WalkSpeed = 16
table.remove(spawns, spawnindex)
torso.CFrame = CFrame.new(spawn.Position + Vector3.new(0, 3, 0))
local matchtag = Instance.new("StringValue")
matchtag.Name = "MatchTag"
matchtag.Parent = player.Character
local backpack = player:FindFirstChild("Backpack")
if backpack then
if player == culprit then
humanoid.WalkSpeed = 18
local machete = serverstorage:WaitForChild("Machete"):clone()
machete.Parent = backpack
local lantern = serverstorage:WaitForChild("Lantern"):clone()
lantern.Parent = backpack
event:FireClient(player, "Class", "Culprit")
elseif player == officer then
local machete = serverstorage:WaitForChild("Machete"):clone()
event:FireClient(player, "Class", "Officer")
elseif player == agent then
local machete = serverstorage:WaitForChild("Machete"):clone()
event:FireClient(player, "Class", "Agent")
else
local lantern = serverstorage:WaitForChild("Lantern"):clone()
lantern.Parent = backpack
event:FireClient(player, "Class", "Victim")
end
end
end
end
end
spawnsmodel:remove()
--wait for round end conditions
local localtimer = roundtime
while localtimer > 0 do
wait(1)
localtimer = localtimer - 1
timertag.Value = localtimer
activecontestants = {}
culpritactive = false
for _, player in pairs(contestants) do
if player then
local character = player.Character
if character then
local matchtag = character:FindFirstChild("MatchTag")
local humanoid = character:FindFirstChild("Humanoid")
if matchtag and humanoid and humanoid.Health > 0 then
if player == culprit then
culpritactive = true
end
table.insert(activecontestants, player)
end
end
end
end
if #activecontestants <= 1 or not culpritactive then
break
end
end
--sort game result
local gameresult = "PlayersWin"
if culpritactive then
if #activecontestants >= 2 then
event:FireAllClients("Result", "PlayersWin")
else
gameresult = "CulpritWin"
event:FireAllClients("Result", "CulpritWin")
end
end
for _, v in pairs(game.Workspace:GetChildren()) do
if v then
if v.Name == "Pickup" or v.className == "Hat" then
v:remove()
end
end
end
--teleport plays back to lobby
local lobbyspawns = {}
for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
if v and v.Name == "SpawnLocation" then
table.insert(lobbyspawns, v)
end
end
for _, player in pairs(activecontestants) do
if player then
if player.Character then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid:UnequipTools()
end
end
local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
player.Character:MoveTo(randomspawn.Position)
local backpack = player:FindFirstChild("Backpack")
if backpack then
backpack:ClearAllChildren()
end
end
end
culprit = nil
--intermission wait
statustag.Value = "Intermission"
timertag.Value = intermissiontime
local localtimer = intermissiontime
while localtimer > 0 do
wait(1)
localtimer = localtimer - 1
timertag.Value = localtimer
end
end
I do realize the site doesnt like the script, some of it will be in a script box and others wont. Sorry :(

1 comment
If you "know a ton more about scripting", you should need us to organize it...