Advertisement
sriyanto

EventHandling

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