Advertisement
qsenko1

TrailScript Serverside

Apr 24th, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. -- Variables
  2. local debounces = {}
  3. local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest(~~)")
  4. local Bluetrail = game.ReplicatedStorage.Trails:FindFirstChild("Blue")
  5. local Redtrail = game.ReplicatedStorage.Trails:FindFirstChild("Red")
  6. -- Functions
  7.  
  8. local function EquipTrail(plr,trail)
  9.     local char = plr.Character
  10.    
  11.     if trail ~= nil and char ~= nil then
  12.        
  13.         if char.HumanoidRootPart:FindFirstChild("Attachment") then char.HumanoidRootPart.Attachment:Destroy() end
  14.         if char.Head:FindFirstChild("Attachment") then char.Head.Attachment:Destroy() end
  15.        
  16.         if char:FindFirstChild(plr.Name.."'s Trail") then char[plr.Name.."'s Trail"]:Destroy() end
  17.        
  18.         trail.Name = plr.Name.."'s Trail"
  19.        
  20.         local attachment1 = Instance.new("Attachment",char:FindFirstChild("HumanoidRootPart"))
  21.         local attachment2 = Instance.new("Attachment",char:FindFirstChild("Head"))
  22.        
  23.         trail.Attachment0 = attachment1
  24.         trail.Attachment1 = attachment2
  25.        
  26.         trail.Parent = char
  27.     end
  28. end
  29.  
  30. -- Player Joins
  31.  
  32. game.Players.PlayerAdded:Connect(function(player)
  33.    
  34.     -- Stats for the player
  35.     local trailInventory = Instance.new("Folder",player); trailInventory.Name = "TrailInventory"
  36.     local equippedTrail = Instance.new("StringValue",player); equippedTrail.Name = "EquippedTrail"
  37.    
  38.     -- Player's character added to the game
  39.    
  40.     player.CharacterAdded:Connect(function(char)
  41.    
  42.         if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
  43.             EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
  44.         end
  45.     end)
  46.    
  47.     -- If the equipped Value changed, it will get their trail
  48.     equippedTrail.Changed:Connect(function()
  49.         if equippedTrail.Value ~= nil then
  50.             if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
  51.                 EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
  52.             end
  53.         end
  54.         if equippedTrail.Value == "" then
  55.            
  56.             local char = player.Character
  57.            
  58.             local root = char:FindFirstChild("HumanoidRootPart")
  59.             local head = char:FindFirstChild("Head")
  60.            
  61.             if root:FindFirstChild("Attachment") then root.Attachment:Destroy() end
  62.             if head:FindFirstChild("Attachment") then head.Attachment:Destroy() end
  63.  
  64.             if char:FindFirstChild(player.Name.."'s Trail") then char[player.Name.."'s Trail"]:Destroy() end
  65.         end
  66.     end)
  67.    
  68.     -- loaded up the data
  69.  
  70.     local data
  71.  
  72.     local success, errorMsg = pcall(function()
  73.         data = dataStore:GetAsync(player.UserId)
  74.     end)
  75.  
  76.     if data ~= nil then
  77.         if data.EquippedTrail then
  78.             equippedTrail.Value = data.EquippedTrail
  79.         end
  80.         if data.Trails then
  81.             for index,trail in pairs(data.Trails) do
  82.                 local bool = Instance.new("BoolValue",trailInventory); bool.Name = trail
  83.             end
  84.         end
  85.     else
  86.         -- New player
  87.         print("A new challenger approch")
  88.     end
  89.  
  90.     -- This event will fire the client to update the gui
  91.     game.ReplicatedStorage.Events.SendData:FireClient(player,data)
  92. end)
  93.  
  94. game.Players.PlayerRemoving:Connect(function(player)
  95.     -- Data holder
  96.     local data = {}
  97.    
  98.     data.EquippedTrail = player.EquippedTrail.Value
  99.    
  100.     data.Trails = {}
  101.    
  102.     for i,v in pairs(player.TrailInventory:GetChildren()) do
  103.         table.insert(data.Trails,v.Name)
  104.     end
  105.    
  106.     local success, errorMsg = pcall(function()
  107.         dataStore:SetAsync(player.UserId,data)
  108.     end)
  109.    
  110.     -- if its succeed then the data is saved else we got an error
  111.    
  112.     if success then
  113.         print("Trails Succesfully saved")
  114.     else
  115.         print(errorMsg)
  116.     end
  117.    
  118. end)
  119.  
  120. game:BindToClose(function()
  121.     for index, player in pairs(game.Players:GetPlayers()) do
  122.         local data = {}
  123.  
  124.         data.EquippedTrail = player.EquippedTrail.Value
  125.  
  126.         data.Trails = {}
  127.  
  128.         for i,v in pairs(player.TrailInventory:GetChildren()) do
  129.             table.insert(data.Trails,v.Name)
  130.         end
  131.  
  132.         local success, errorMsg = pcall(function()
  133.             dataStore:SetAsync(player.UserId,data)
  134.         end)
  135.  
  136.         -- if its succeed then the data is saved else we got an error
  137.  
  138.         if success then
  139.             print("Succesfully saved")
  140.         else
  141.             print(errorMsg)
  142.         end
  143.     end
  144. end)
  145.  
  146. -- Server Events
  147.  
  148. game.ReplicatedStorage.Events.BuyItem2.OnServerInvoke = function(player, RedtrailName)
  149.     if debounces[player] == nil then
  150.         debounces[player] = true
  151.     --print("RemoteFunction fired")
  152.     local ifsInInventory
  153.    
  154.     if player.TrailInventory:FindFirstChild("Red") then
  155.         ifsInInventory = true
  156.     end    
  157.    
  158.     if Redtrail then
  159.         if Redtrail:FindFirstChild("Price") then
  160.             if not ifsInInventory then
  161.                 -- Check if we can buy the trail
  162.                 if player.leaderstats.Coins.Value >= Redtrail.Price.Value then
  163.                     print(player.Name.." brought the ".. Redtrail.Name.." trail")
  164.  
  165.                     player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - Redtrail.Price.Value
  166.  
  167.                     local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = Redtrail.Name
  168.                
  169.                     return "Brought"
  170.                 else
  171.                     return "NotEnough"     
  172.                 end
  173.                 else
  174.                 -- You already owned it
  175.                 if player.EquippedTrail.Value ~= Redtrail.Name and ifsInInventory then                             
  176.                    
  177.                     player.EquippedTrail.Value = Redtrail.Name
  178.                     --print(player.Name.." equipped the ".. Redtrail.Name.." trail")   
  179.                     debounces = {}
  180.                     return "Equip"                                     
  181.                 --end                                      
  182.                     else --if   player.EquippedTrail.Value == Redtrail.Name and ifsInInventory then
  183.                     player.EquippedTrail.Value = ""
  184.                     --print(player.Name.." unequipped the ".. Redtrail.Name.." trail")
  185.                     debounces = {}
  186.                     return "Unequip"
  187.                 end                    
  188.              end
  189.           end
  190.        end
  191.     end
  192. end
  193.  
  194. game.ReplicatedStorage.Events.BuyItem.OnServerInvoke = function(player, BluetrailName)
  195.     if debounces[player] == nil then
  196.     debounces[player] = true
  197.     print("RemoteFunction fired")
  198.     local ifsInInventory   
  199.     if player.TrailInventory:FindFirstChild("Blue") then
  200.         ifsInInventory = true
  201.     end
  202.  
  203.     if Bluetrail then
  204.         if Bluetrail:FindFirstChild("Price") then
  205.             if not ifsInInventory then
  206.                 -- Check if we can buy the trail
  207.                 if player.leaderstats.Coins.Value >= Bluetrail.Price.Value then
  208.                     print(player.Name.." brought the ".. Bluetrail.Name.." trail")
  209.                     player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - Bluetrail.Price.Value
  210.                     local bool2 = Instance.new("BoolValue",player.TrailInventory); bool2.Name = Bluetrail.Name
  211.                     return "Brought"
  212.                 else
  213.                     return "NotEnough"     
  214.                 end
  215.             else
  216.                 -- You already owned it
  217.                 if player.EquippedTrail.Value ~= Bluetrail.Name and ifsInInventory then                            
  218.                     player.EquippedTrail.Value = Bluetrail.Name
  219.                 --  print(player.Name.." equipped the ".. Bluetrail.Name.." trail")    
  220.                     debounces = {}
  221.                     return "Equip"                 
  222.                 else                       
  223.                     player.EquippedTrail.Value = ""
  224.                     --print(player.Name.." unequipped the ".. Bluetrail.Name.." trail")
  225.                     debounces = {}
  226.                     return "Unequip"
  227.                        
  228.                     end
  229.                 end        
  230.             end
  231.         end
  232.     end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement