Advertisement
sriyanto

Event Handling(ServerScriptService)

Apr 6th, 2024 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. local keyModule = require(script.Parent:WaitForChild("Game Logic").KeyModule)
  2. local roundModule = require(script.Parent:WaitForChild("Game Logic").RoundModule)
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local DataStore = DataStoreService:GetDataStore("MyDataStore2")
  5. game.Players.PlayerAdded:Connect(function(player)
  6.     -- Insert a value tag inside the player so we can tell whether they are in the menu or not. This way we can tell which players are
  7.     -- active and ready to play, and prevents AFK players from joining a round when it starts, as they are still in the Menu GUI.
  8.     local inMenu = Instance.new("BoolValue")
  9.     inMenu.Name = "InMenu"
  10.     inMenu.Parent = player
  11.     -- AddToTutorial
  12.     local piggyTokens = Instance.new("IntValue")
  13.     piggyTokens.Name = "Tokens"
  14.     piggyTokens.Value = 1000
  15.     piggyTokens.Parent = player
  16.     local trapInventory = Instance.new("Folder")
  17.     trapInventory.Name = "TrapInventory"
  18.     trapInventory.Parent = player
  19.     local skinInventory = Instance.new("Folder")
  20.     skinInventory.Name = "SkinInventory"
  21.     skinInventory.Parent = player
  22.     local equippedTrap = Instance.new("StringValue")
  23.     equippedTrap.Name = "EquippedTrap"
  24.     equippedTrap.Parent = player
  25.     local equippedSkin = Instance.new("StringValue")
  26.     equippedSkin.Name = "EquippedSkin"
  27.     equippedSkin.Parent = player
  28.     local data = DataStore:GetAsync(player.UserId)
  29.     if data ~= nil then
  30.         if data.EquippedTrap then
  31.             equippedTrap.Value = data.EquippedTrap
  32.             print("loaded equipped trap : "..tostring(data.EquippedTrap))
  33.         end
  34.         if data.EquippedSkin then
  35.             equippedSkin.Value = data.EquippedSkin
  36.             print("loaded equipped skin : "..tostring(data.EquippedSkin))
  37.         end
  38.         if data.Tokens then
  39.             piggyTokens.Value = data.Tokens
  40.             print(data.Tokens.." tokens loaded")
  41.         end
  42.         if data.Skins then
  43.             print("bring it")
  44.             for i, v in pairs(data.Skins) do
  45.                 local val = Instance.new("StringValue")
  46.                 val.Name = v
  47.                 val.Parent = skinInventory
  48.             end
  49.         end
  50.         if data.Traps then
  51.             print("bring it")
  52.             for i, v in pairs(data.Traps) do
  53.                 local val = Instance.new("StringValue")
  54.                 val.Name = v
  55.                 val.Parent = trapInventory
  56.             end
  57.         end    
  58.     end
  59.     game.ReplicatedStorage.SendData:FireClient(player,data)
  60.     player.CharacterAdded:Connect(function(char)
  61.         char.Humanoid.Died:Connect(function()
  62.             if char:FindFirstChild("HumanoidRootPart") then
  63.                 if player:FindFirstChild("Contestant") and game.Workspace:FindFirstChild("Map") then -- added
  64.                     keyModule.DropTools(player,game.Workspace.Map,char.HumanoidRootPart.Position)
  65.                 end
  66.             end
  67.             if player:FindFirstChild("Contestant") then
  68.                 player.Contestant:Destroy()
  69.             elseif player:FindFirstChild("Piggy") then
  70.                 player.Piggy:Destroy()
  71.             end
  72.         end)
  73.     end)
  74. end)
  75. game.Players.PlayerRemoving:Connect(function(player)
  76.     local data = {}
  77.     data.Tokens = player.Tokens.Value
  78.     data.EquippedTrap = player.EquippedTrap.Value
  79.     data.EquippedSkin = player.EquippedSkin.Value
  80.     data.Skins = {}
  81.     data.Traps = {}
  82.     for i, v in pairs(player.SkinInventory:GetChildren()) do
  83.         table.insert(data.Skins,v.Name)
  84.         print(v.Name)
  85.     end
  86.     for i, v in pairs(player.TrapInventory:GetChildren()) do
  87.         table.insert(data.Traps,v.Name)
  88.         print(v.Name)
  89.     end
  90.     DataStore:SetAsync(player.UserId,data)
  91.     print("saved")
  92. end)
  93. game:BindToClose(function()
  94.     for i, player in pairs(game.Players:GetPlayers()) do
  95.         local data = {}
  96.         data.Tokens = player.Tokens.Value
  97.         data.EquippedTrap = player.EquippedTrap.Value
  98.         data.EquippedSkin = player.EquippedSkin.Value
  99.         data.Skins = {}
  100.         data.Traps = {}
  101.         for i, v in pairs(player.SkinInventory:GetChildren()) do
  102.             table.insert(data.Skins,v.Name)
  103.             print(v.Name)
  104.         end
  105.         for i, v in pairs(player.TrapInventory:GetChildren()) do
  106.             table.insert(data.Traps,v.Name)
  107.             print(v.Name)
  108.         end
  109.         DataStore:SetAsync(player.UserId,data)
  110.         print("saved")
  111.     end
  112. end)
  113. local trapDebounce = false
  114. game.ReplicatedStorage.PlaceTrap.OnServerEvent:Connect(function(player)
  115.     if player:FindFirstChild("Piggy") then
  116.         if player:FindFirstChild("TrapCount") then
  117.             if not trapDebounce then
  118.                 trapDebounce = true
  119.                 if player.TrapCount.Value > 0 then
  120.                     if game.Workspace:FindFirstChild("Map") then
  121.                         player.TrapCount.Value = player.TrapCount.Value - 1
  122.                         local trap
  123.                             if player.EquippedTrap.Value ~= "" then
  124.                             if game.ReplicatedStorage.Traps:FindFirstChild(player.EquippedTrap.Value) then
  125.                                 trap = game.ReplicatedStorage.Traps[player.EquippedTrap.Value]:Clone()
  126.                             end
  127.                         else
  128.                             trap = game.ReplicatedStorage.Traps.Bear_Trap:Clone()
  129.                         end
  130.                         trap.CFrame = player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3.5,0)
  131.                         trap.Parent = game.Workspace:FindFirstChild("Map")                 
  132.                     end
  133.                 end    
  134.                 wait(5)
  135.                 trapDebounce = false
  136.             end
  137.         end
  138.     end
  139. end)
  140. game.ReplicatedStorage.MenuPlay.OnServerEvent:Connect(function(player)
  141.     print("test")
  142.     if player:FindFirstChild("InMenu") then
  143.         -- Remove the InMenu tag so that you can be counted as an active player.
  144.         player.InMenu:Destroy()
  145.     end
  146.     if game.ServerStorage.GameValues.GameInProgress.Value == true then
  147.         local contestant = Instance.new("BoolValue")
  148.         contestant.Name = "Contestant"
  149.         contestant.Parent = player
  150.         game.ReplicatedStorage.ToggleCrouch:FireClient(player,true)
  151.         roundModule.TeleportPlayers({player},game.Workspace:FindFirstChild("Map").PlayerSpawns:GetChildren())  
  152.     end
  153. end)
  154. game.ReplicatedStorage.BuyTrap.OnServerInvoke = function(player,trapName)
  155.     local trap = game.ReplicatedStorage.Traps:FindFirstChild(trapName)
  156.     if trap then
  157.         if trap:FindFirstChild("Cost") then
  158.             if not player.TrapInventory:FindFirstChild(trapName) then
  159.                 if trap.Cost.Value <= player.Tokens.Value then
  160.                     print("You can buy this")      
  161.                     player.Tokens.Value = player.Tokens.Value - trap.Cost.Value        
  162.                     local stringValue = Instance.new("StringValue")
  163.                     stringValue.Name = trap.Name
  164.                     stringValue.Parent = player.TrapInventory          
  165.                     return "bought"        
  166.                 else
  167.                     print("Not enough money")
  168.                     return "failed"
  169.                 end
  170.             else
  171.                 print("You already own this item - equipping")
  172.                 player.EquippedTrap.Value = trapName
  173.                 return "equipped"
  174.             end
  175.         end
  176.     else
  177.         print("No Trap of such Name found")
  178.         return "failed"
  179.     end
  180. end
  181. game.ReplicatedStorage.BuySkin.OnServerInvoke = function(player,skinName)
  182.     local skin = game.ReplicatedStorage.Skins:FindFirstChild(skinName)
  183.     if skin then
  184.         if skin:FindFirstChild("Cost") then
  185.             if not player.SkinInventory:FindFirstChild(skinName) then
  186.                 if skin.Cost.Value <= player.Tokens.Value then
  187.                     print("You can buy this")      
  188.                     player.Tokens.Value = player.Tokens.Value - skin.Cost.Value            
  189.                     local stringValue = Instance.new("StringValue")
  190.                     stringValue.Name = skin.Name
  191.                     stringValue.Parent = player.SkinInventory              
  192.                     return "bought"            
  193.                 else
  194.                     print("Not enough money")
  195.                     return "failed"
  196.                 end
  197.             else
  198.                 print("You already own this item - equipping")
  199.                 player.EquippedSkin.Value = skinName
  200.                 return "equipped"
  201.             end
  202.         end
  203.     else
  204.         print("No Skin of such Name found")
  205.         return "failed"
  206.     end
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement