Advertisement
HowToRoblox

GameServer

Nov 18th, 2022 (edited)
1,961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.80 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local cashDS = dss:GetDataStore("CASH DATA")
  3.  
  4. local rs = game.ReplicatedStorage:WaitForChild("MurderMysteryReplicatedStorage")
  5. local config = require(rs:WaitForChild("CONFIGURATION"))
  6. local re = rs:WaitForChild("RemoteEvent")
  7. local maps = rs:WaitForChild("Maps")
  8. local weapons = rs:WaitForChild("Weapons")
  9.  
  10. local status = Instance.new("StringValue")
  11. status.Name = "STATUS"
  12. status.Parent = rs
  13.  
  14. local alivePlayers = {}
  15. local sheriffs = {}
  16. local murderers = {}
  17.  
  18. local sheriffWeaponCooldown = {}
  19. local murdererWeaponCooldown = {}
  20.  
  21.  
  22. function saveData(plr)
  23.    
  24.     if not plr:FindFirstChild("FAILED TO LOAD DATA") then
  25.        
  26.         local cash = plr.leaderstats.Cash.Value
  27.        
  28.         local success, err
  29.         while not success do
  30.             warn(err)
  31.            
  32.             success, err = pcall(function()
  33.                 cashDS:SetAsync(plr.UserId .. "Cash", cash)
  34.             end)
  35.             task.wait(1)
  36.         end
  37.     end
  38. end
  39.  
  40. game.Players.PlayerRemoving:Connect(function(plr)
  41.     task.spawn(saveData, plr)
  42.    
  43.     if table.find(alivePlayers, plr) then
  44.         table.remove(alivePlayers, table.find(alivePlayers, plr))
  45.     elseif table.find(murderers, plr) then
  46.         table.remove(murderers, table.find(murderers, plr))
  47.     elseif table.find(sheriffs, plr) then
  48.         table.remove(sheriffs, table.find(sheriffs, plr))
  49.     end
  50. end)
  51.  
  52. game:BindToClose(function()
  53.    
  54.     for i, plr in pairs(game.Players:GetPlayers()) do
  55.         saveData(plr)
  56.     end
  57. end)
  58.  
  59.  
  60. game.Players.PlayerAdded:Connect(function(plr)
  61.    
  62.     local ls = Instance.new("Folder")
  63.     ls.Name = "leaderstats"
  64.     ls.Parent = plr
  65.    
  66.     local cash = Instance.new("IntValue")
  67.     cash.Name = "Cash"
  68.     cash.Parent = ls
  69.    
  70.     local success, cashData = pcall(function()
  71.         return cashDS:GetAsync(plr.UserId .. "Cash")
  72.     end)
  73.    
  74.     if success then
  75.         cash.Value = cashData or 0
  76.         print("Data successfully loaded for " .. plr.Name)
  77.        
  78.     else
  79.         warn("Data not loaded for " .. plr.Name)
  80.        
  81.         local failedToLoad = Instance.new("StringValue")
  82.         failedToLoad.Name = "FAILED TO LOAD DATA"
  83.         failedToLoad.Parent = plr
  84.        
  85.         re:FireClient(plr, "FAILED TO LOAD DATA")
  86.     end
  87. end)
  88.  
  89.  
  90. re.OnServerEvent:Connect(function(plr, instruction, tool, camCF, mouseCF)
  91.    
  92.     local char = plr.Character
  93.     if char and char.Humanoid.Health > 0 then
  94.  
  95.         if instruction == "SHERIFF SHOOT" then
  96.  
  97.             if table.find(sheriffs, plr) and not sheriffWeaponCooldown[plr] and tool.Parent == char then
  98.                 sheriffWeaponCooldown[plr] = true
  99.  
  100.                 tool.Handle.ShootSound:Play()
  101.  
  102.                 local rayParams = RaycastParams.new()
  103.                 rayParams.FilterType = Enum.RaycastFilterType.Blacklist
  104.                 rayParams.FilterDescendantsInstances = {char}
  105.                
  106.                 local cframe = CFrame.new(camCF.Position, mouseCF.Position)
  107.                 local ray = workspace:Raycast(camCF.Position, cframe.LookVector * config.RaycastDepth, rayParams)
  108.  
  109.                 if ray then
  110.                     local secondRay = workspace:Raycast(char.HumanoidRootPart.Position, ray.Position, rayParams)
  111.                    
  112.                     if secondRay.Instance ~= ray.Instance and secondRay.Instance.Parent ~= ray.Instance.Parent and secondRay.Instance.Parent.Parent ~= ray.Instance.Parent.Parent then
  113.                         ray = secondRay
  114.                     end
  115.                 end
  116.  
  117.                 local beamContainer = Instance.new("Part")
  118.                 beamContainer.Name = "BEAM CONTAINER"
  119.                 beamContainer.CanCollide = false
  120.                 beamContainer.Transparency = 1
  121.                 beamContainer.Anchored = true
  122.                 beamContainer.Parent = workspace
  123.  
  124.                 local tracer = Instance.new("Beam")
  125.                 tracer.Color = ColorSequence.new{ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 160, 40)), ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 160, 40))}
  126.                 tracer.FaceCamera = true
  127.                 tracer.LightEmission = 5
  128.                
  129.                 local atch0, atch1 = Instance.new("Attachment"), Instance.new("Attachment")
  130.                 atch0.Name, atch0.Name = "A0", "A1"
  131.                 atch0.Parent, atch1.Parent = beamContainer, beamContainer
  132.                 atch0.WorldPosition, atch1.WorldPosition = tool.Handle.Position, ray and ray.Position or mouseCF.LookVector * config.RaycastDepth
  133.                 tracer.Attachment0, tracer.Attachment1 = atch0, atch1
  134.                 tracer.Width0, tracer.Width1 = 0.1, 0.1
  135.                
  136.                 tracer.Parent = beamContainer
  137.                 game:GetService("Debris"):AddItem(beamContainer, 0.06)
  138.                
  139.                 local hitHumanoid = nil
  140.                 if ray then
  141.                     hitHumanoid = ray.Instance.Parent:FindFirstChild("Humanoid") or ray.Instance.Parent.Parent:FindFirstChild("Humanoid")
  142.                 end
  143.                
  144.                 if hitHumanoid and hitHumanoid.Parent ~= char then
  145.  
  146.                     local targetPlr = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
  147.                     if targetPlr then
  148.  
  149.                         hitHumanoid.Health = 0
  150.  
  151.                         if (table.find(alivePlayers, targetPlr) or table.find(sheriffs, targetPlr)) and not table.find(murderers, targetPlr) then
  152.                             char.Humanoid.Health = 0
  153.                         end
  154.                     end
  155.                 end
  156.  
  157.                 task.wait(config.SheriffWeaponCooldown)
  158.                 sheriffWeaponCooldown[plr] = false
  159.             end
  160.  
  161.  
  162.         elseif instruction == "MURDERER STAB" then
  163.  
  164.             if table.find(murderers, plr) and not murdererWeaponCooldown[plr] and tool.Parent == char then
  165.                 murdererWeaponCooldown[plr] = true
  166.                
  167.                 tool.Handle.SwingSound:Play()
  168.  
  169.                 local rayParams = RaycastParams.new()
  170.                 rayParams.FilterType = Enum.RaycastFilterType.Blacklist
  171.                 rayParams.FilterDescendantsInstances = {char}
  172.  
  173.                 local ray = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * config.MurdererWeaponStabRange)
  174.  
  175.                 if ray and ray.Instance.Parent:FindFirstChild("Humanoid") then
  176.  
  177.                     local targetPlr = game.Players:GetPlayerFromCharacter(ray.Instance.Parent)
  178.                     if table.find(alivePlayers, targetPlr) or table.find(sheriffs, targetPlr) then
  179.  
  180.                         ray.Instance.Parent.Humanoid.Health = 0
  181.  
  182.                         tool.Handle.KillSound:Play()
  183.                     end
  184.                 end
  185.  
  186.                 task.wait(config.MurdererWeaponCooldown)
  187.                 murdererWeaponCooldown[plr] = false
  188.             end
  189.  
  190.  
  191.         elseif instruction == "MURDERER THROW" then
  192.  
  193.             if table.find(murderers, plr) and not murdererWeaponCooldown[plr] and tool.Parent == char then
  194.                 murdererWeaponCooldown[plr] = true
  195.  
  196.                 local knifeClone = tool.Handle:Clone()
  197.                
  198.                 local cframe = CFrame.new(camCF.Position, mouseCF.Position)
  199.  
  200.                 re:FireAllClients("DUMMY KNIFE", tool, cframe.LookVector * config.MurdererWeaponThrowVelocity)
  201.                
  202.                 knifeClone.Velocity = cframe.LookVector * config.MurdererWeaponThrowVelocity
  203.                
  204.                 tool.Handle.Transparency = 1
  205.                 knifeClone.Transparency = 1
  206.                
  207.                 tool.Handle.ThrowSound:Play()
  208.                
  209.                 knifeClone.Parent = workspace
  210.  
  211.                 knifeClone.CFrame = CFrame.new(tool.Handle.Position, cframe.LookVector * config.MurdererWeaponThrowVelocity)
  212.                
  213.                
  214.                 knifeClone.Touched:Connect(function(hit)
  215.                     if hit.Parent ~= tool.Parent and hit.Parent.Parent ~= tool.Parent then 
  216.  
  217.                         local humanoid = hit.Parent:FindFirstChild("Humanoid")
  218.  
  219.                         if humanoid then
  220.                             humanoid.Health = 0
  221.                            
  222.                             knifeClone.KillSound:Play()
  223.                            
  224.                             knifeClone.Transparency = 1
  225.                             knifeClone.KillSound.Ended:Wait()
  226.                         end
  227.                        
  228.                         knifeClone:Destroy()
  229.                     end
  230.                 end)
  231.  
  232.                 task.wait(config.MurdererWeaponCooldown)
  233.                 murdererWeaponCooldown[plr] = false
  234.  
  235.                 tool.Handle.Transparency = 0
  236.             end
  237.         end
  238.     end
  239. end)
  240.  
  241.  
  242. while true do
  243.    
  244.     while #game.Players:GetPlayers() < config.MinPlayersToStart do
  245.         status.Value = "Need " .. config.MinPlayersToStart .. " players to start"
  246.        
  247.         task.wait(0.5)  
  248.     end
  249.    
  250.     for i = config.IntermissionTime, 0, -1 do
  251.         status.Value = "Starting in " .. i .. "s"
  252.         task.wait(1)
  253.     end
  254.    
  255.    
  256.     local chosenMap = maps:GetChildren()[Random.new():NextInteger(1, #maps:GetChildren())]:Clone()
  257.     chosenMap.Parent = workspace
  258.    
  259.     status.Value = "Map chosen: " .. chosenMap.Name
  260.    
  261.     task.wait(config.TimeBetweenMapAndSpawn)
  262.    
  263.    
  264.     if #game.Players:GetPlayers() >= config.MinPlayersToStart then
  265.         alivePlayers = {}
  266.        
  267.         for i, plr in pairs(game.Players:GetPlayers()) do
  268.            
  269.             local char = plr.Character
  270.            
  271.             if char and char.Humanoid.Health > 0 then
  272.                 table.insert(alivePlayers, plr)
  273.             end
  274.         end
  275.        
  276.        
  277.         murderers = {}
  278.        
  279.         for i = 1, config.Murderers do
  280.            
  281.             local chosenMurderer = nil
  282.             while not chosenMurderer or table.find(murderers, chosenMurderer) do
  283.                 chosenMurderer = alivePlayers[Random.new():NextInteger(1, #alivePlayers)]
  284.             end
  285.             table.insert(murderers, chosenMurderer)
  286.             table.remove(alivePlayers, table.find(alivePlayers, chosenMurderer))
  287.         end
  288.        
  289.         sheriffs = {}
  290.        
  291.         for i = 1, config.Sheriffs do
  292.  
  293.             local chosenSheriff = nil
  294.             while not chosenSheriff or table.find(sheriffs, chosenSheriff) do
  295.                 chosenSheriff = alivePlayers[Random.new():NextInteger(1, #alivePlayers)]
  296.             end
  297.             table.insert(sheriffs, chosenSheriff)
  298.             table.remove(alivePlayers, table.find(alivePlayers, chosenSheriff))
  299.         end
  300.        
  301.        
  302.         for i, plr in pairs(game.Players:GetPlayers()) do
  303.            
  304.             local role = table.find(alivePlayers, plr) and "INNOCENT" or table.find(murderers, plr) and "MURDERER" or table.find(sheriffs, plr) and "SHERIFF"
  305.            
  306.             if role then
  307.                 re:FireClient(plr, "ROLE CHOSEN", role)
  308.                
  309.                 local char = plr.Character
  310.                
  311.                 char.Humanoid.Died:Connect(function()
  312.                    
  313.                     role = table.find(alivePlayers, plr) and "INNOCENT" or table.find(murderers, plr) and "MURDERER" or table.find(sheriffs, plr) and "SHERIFF"
  314.                    
  315.                     if role == "INNOCENT" then
  316.                         table.remove(alivePlayers, table.find(alivePlayers, plr))
  317.                     elseif role == "MURDERER" then
  318.                         table.remove(murderers, table.find(alivePlayers, plr))
  319.                     elseif role == "SHERIFF" then
  320.                         table.remove(sheriffs, table.find(alivePlayers, plr))
  321.                        
  322.                         local sheriffWeapon = plr.Backpack:FindFirstChild("Sheriff") or char:FindFirstChild("Sheriff")
  323.                         sheriffWeapon.Enabled = false
  324.                         sheriffWeapon.Handle.CFrame = char.HumanoidRootPart.CFrame
  325.                         sheriffWeapon.Handle.Anchored = true
  326.                         sheriffWeapon.Parent = workspace
  327.                     end
  328.                 end)
  329.                
  330.                
  331.                 local randomSpawn = chosenMap.Spawns:GetChildren()[Random.new():NextInteger(1, #chosenMap.Spawns:GetChildren())]
  332.                
  333.                 local hrp = char.HumanoidRootPart
  334.                 hrp.CFrame = randomSpawn.CFrame
  335.                
  336.                 randomSpawn:Destroy()
  337.             end
  338.         end
  339.        
  340.         task.wait(config.TimeBeforeReceivingWeapons)
  341.        
  342.         for i, murderer in pairs(murderers) do
  343.             weapons.Murderer:Clone().Parent = murderer.Backpack
  344.         end
  345.         for i, sheriff in pairs(sheriffs) do
  346.             local sheriffWeapon = weapons.Sheriff:Clone()
  347.             sheriffWeapon.Parent = sheriff.Backpack
  348.            
  349.             sheriffWeapon.Handle.Touched:Connect(function(hit)
  350.                 if sheriffWeapon.Parent == workspace then
  351.  
  352.                     if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) and hit.Parent.Humanoid.Health > 0 then
  353.                         local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
  354.                        
  355.                         if table.find(alivePlayers, plr) then
  356.                            
  357.                             sheriffWeapon.Enabled = true
  358.                             sheriffWeapon.Handle.Anchored = false
  359.                             sheriffWeapon.Parent = game.Players:GetPlayerFromCharacter(hit.Parent).Backpack
  360.                            
  361.                             table.insert(sheriffs, plr)
  362.                             table.remove(table.find(alivePlayers, plr))
  363.                         end
  364.                     end
  365.                 end
  366.             end)
  367.         end
  368.        
  369.        
  370.         local winner = "INNOCENTS"
  371.        
  372.         local gameStart = tick()
  373.        
  374.         while tick() - gameStart < config.RoundTime do
  375.             task.wait(0.2)
  376.            
  377.             local roundEnds = math.round(config.RoundTime - (tick() - gameStart))
  378.            
  379.             local mins = math.floor(roundEnds / 60)
  380.             local secs = roundEnds - (mins*60)
  381.             mins = tostring(mins)
  382.             secs = tostring(secs)
  383.            
  384.             if string.len(mins) < 2 then
  385.                 mins = "0" .. mins
  386.             end
  387.             if string.len(secs) < 2 then
  388.                 secs = "0" .. secs
  389.             end
  390.            
  391.             status.Value = mins .. ":" .. secs
  392.            
  393.             if #alivePlayers == 0 and #sheriffs == 0 then
  394.                 winner = "MURDERERS"
  395.                 break
  396.             elseif #murderers == 0 then
  397.                 winner = "SHERIFFS"
  398.                 break
  399.             end
  400.         end
  401.        
  402.         if winner == "MURDERERS" then
  403.             status.Value = "The murderer killed everyone!"
  404.            
  405.             for i, murderer in pairs(murderers) do
  406.                 murderer.leaderstats.Cash.Value += config.MurderersWinReward
  407.             end
  408.            
  409.         elseif winner == "SHERIFFS" then
  410.             status.Value = "The sheriff killed the murderer!"
  411.            
  412.             for i, sheriff in pairs(sheriffs) do
  413.                 sheriff.leaderstats.Cash.Value += config.SheriffKillReward
  414.             end
  415.             for i, alivePlr in pairs(alivePlayers) do
  416.                 alivePlr.leaderstats.Cash.Value += config.InnocentsSurviveReward
  417.             end
  418.            
  419.         else
  420.             status.Value = "The innocents win by surviving long enough!"
  421.            
  422.             for i, sheriff in pairs(sheriffs) do
  423.                 sheriff.leaderstats.Cash.Value += config.InnocentsSurviveReward
  424.             end
  425.             for i, alivePlr in pairs(alivePlayers) do
  426.                 alivePlr.leaderstats.Cash.Value += config.InnocentsSurviveReward
  427.             end
  428.         end
  429.        
  430.         for i, plr in pairs(murderers) do
  431.             plr:LoadCharacter()
  432.         end
  433.         for i, plr in pairs(sheriffs) do
  434.             plr:LoadCharacter()
  435.         end
  436.         for i, plr in pairs(alivePlayers) do
  437.             plr:LoadCharacter()
  438.         end
  439.        
  440.         chosenMap:Destroy()
  441.        
  442.         alivePlayers = {}
  443.         sheriffs = {}
  444.         murderers = {}
  445.        
  446.         task.wait(config.TimeAfterRoundEnds)
  447.     end
  448. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement