Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local debounces = {}
- local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest(~~)")
- local Bluetrail = game.ReplicatedStorage.Trails:FindFirstChild("Blue")
- local Redtrail = game.ReplicatedStorage.Trails:FindFirstChild("Red")
- -- Functions
- local function EquipTrail(plr,trail)
- local char = plr.Character
- if trail ~= nil and char ~= nil then
- if char.HumanoidRootPart:FindFirstChild("Attachment") then char.HumanoidRootPart.Attachment:Destroy() end
- if char.Head:FindFirstChild("Attachment") then char.Head.Attachment:Destroy() end
- if char:FindFirstChild(plr.Name.."'s Trail") then char[plr.Name.."'s Trail"]:Destroy() end
- trail.Name = plr.Name.."'s Trail"
- local attachment1 = Instance.new("Attachment",char:FindFirstChild("HumanoidRootPart"))
- local attachment2 = Instance.new("Attachment",char:FindFirstChild("Head"))
- trail.Attachment0 = attachment1
- trail.Attachment1 = attachment2
- trail.Parent = char
- end
- end
- -- Player Joins
- game.Players.PlayerAdded:Connect(function(player)
- -- Stats for the player
- local trailInventory = Instance.new("Folder",player); trailInventory.Name = "TrailInventory"
- local equippedTrail = Instance.new("StringValue",player); equippedTrail.Name = "EquippedTrail"
- -- Player's character added to the game
- player.CharacterAdded:Connect(function(char)
- if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
- EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
- end
- end)
- -- If the equipped Value changed, it will get their trail
- equippedTrail.Changed:Connect(function()
- if equippedTrail.Value ~= nil then
- if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
- EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
- end
- end
- if equippedTrail.Value == "" then
- local char = player.Character
- local root = char:FindFirstChild("HumanoidRootPart")
- local head = char:FindFirstChild("Head")
- if root:FindFirstChild("Attachment") then root.Attachment:Destroy() end
- if head:FindFirstChild("Attachment") then head.Attachment:Destroy() end
- if char:FindFirstChild(player.Name.."'s Trail") then char[player.Name.."'s Trail"]:Destroy() end
- end
- end)
- -- loaded up the data
- local data
- local success, errorMsg = pcall(function()
- data = dataStore:GetAsync(player.UserId)
- end)
- if data ~= nil then
- if data.EquippedTrail then
- equippedTrail.Value = data.EquippedTrail
- end
- if data.Trails then
- for index,trail in pairs(data.Trails) do
- local bool = Instance.new("BoolValue",trailInventory); bool.Name = trail
- end
- end
- else
- -- New player
- print("A new challenger approch")
- end
- -- This event will fire the client to update the gui
- game.ReplicatedStorage.Events.SendData:FireClient(player,data)
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- -- Data holder
- local data = {}
- data.EquippedTrail = player.EquippedTrail.Value
- data.Trails = {}
- for i,v in pairs(player.TrailInventory:GetChildren()) do
- table.insert(data.Trails,v.Name)
- end
- local success, errorMsg = pcall(function()
- dataStore:SetAsync(player.UserId,data)
- end)
- -- if its succeed then the data is saved else we got an error
- if success then
- print("Trails Succesfully saved")
- else
- print(errorMsg)
- end
- end)
- game:BindToClose(function()
- for index, player in pairs(game.Players:GetPlayers()) do
- local data = {}
- data.EquippedTrail = player.EquippedTrail.Value
- data.Trails = {}
- for i,v in pairs(player.TrailInventory:GetChildren()) do
- table.insert(data.Trails,v.Name)
- end
- local success, errorMsg = pcall(function()
- dataStore:SetAsync(player.UserId,data)
- end)
- -- if its succeed then the data is saved else we got an error
- if success then
- print("Succesfully saved")
- else
- print(errorMsg)
- end
- end
- end)
- -- Server Events
- game.ReplicatedStorage.Events.BuyItem2.OnServerInvoke = function(player, RedtrailName)
- if debounces[player] == nil then
- debounces[player] = true
- --print("RemoteFunction fired")
- local ifsInInventory
- if player.TrailInventory:FindFirstChild("Red") then
- ifsInInventory = true
- end
- if Redtrail then
- if Redtrail:FindFirstChild("Price") then
- if not ifsInInventory then
- -- Check if we can buy the trail
- if player.leaderstats.Coins.Value >= Redtrail.Price.Value then
- print(player.Name.." brought the ".. Redtrail.Name.." trail")
- player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - Redtrail.Price.Value
- local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = Redtrail.Name
- return "Brought"
- else
- return "NotEnough"
- end
- else
- -- You already owned it
- if player.EquippedTrail.Value ~= Redtrail.Name and ifsInInventory then
- player.EquippedTrail.Value = Redtrail.Name
- --print(player.Name.." equipped the ".. Redtrail.Name.." trail")
- debounces = {}
- return "Equip"
- --end
- else --if player.EquippedTrail.Value == Redtrail.Name and ifsInInventory then
- player.EquippedTrail.Value = ""
- --print(player.Name.." unequipped the ".. Redtrail.Name.." trail")
- debounces = {}
- return "Unequip"
- end
- end
- end
- end
- end
- end
- game.ReplicatedStorage.Events.BuyItem.OnServerInvoke = function(player, BluetrailName)
- if debounces[player] == nil then
- debounces[player] = true
- print("RemoteFunction fired")
- local ifsInInventory
- if player.TrailInventory:FindFirstChild("Blue") then
- ifsInInventory = true
- end
- if Bluetrail then
- if Bluetrail:FindFirstChild("Price") then
- if not ifsInInventory then
- -- Check if we can buy the trail
- if player.leaderstats.Coins.Value >= Bluetrail.Price.Value then
- print(player.Name.." brought the ".. Bluetrail.Name.." trail")
- player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - Bluetrail.Price.Value
- local bool2 = Instance.new("BoolValue",player.TrailInventory); bool2.Name = Bluetrail.Name
- return "Brought"
- else
- return "NotEnough"
- end
- else
- -- You already owned it
- if player.EquippedTrail.Value ~= Bluetrail.Name and ifsInInventory then
- player.EquippedTrail.Value = Bluetrail.Name
- -- print(player.Name.." equipped the ".. Bluetrail.Name.." trail")
- debounces = {}
- return "Equip"
- else
- player.EquippedTrail.Value = ""
- --print(player.Name.." unequipped the ".. Bluetrail.Name.." trail")
- debounces = {}
- return "Unequip"
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement