Advertisement
qsenko1

TrailScript

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