Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest")
- -- 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 coins = Instance.new("IntValue",player); coins.Name = "Coins"
- 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.Coins then
- coins.Value = data.Coins
- end
- 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.Coins = player.Coins.Value
- 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)
- game:BindToClose(function()
- for index, player in pairs(game.Players:GetPlayers()) do
- local data = {}
- data.Coins = player.Coins.Value
- 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.BuyItem.OnServerInvoke = function(player, trailName)
- local trail = game.ReplicatedStorage.Trails:FindFirstChild(trailName)
- local ifsInInventory
- if player.TrailInventory:FindFirstChild(trailName) then
- ifsInInventory = true
- end
- if trail then
- if trail:FindFirstChild("Price") then
- if not ifsInInventory then
- -- Check if we can buy the trail
- if player.Coins.Value >= trail.Price.Value then
- print(player.Name.." brought the ".. trail.Name.." trail")
- player.Coins.Value = player.Coins.Value - trail.Price.Value
- local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = trail.Name
- return "Brought"
- else
- return "NotEnough"
- end
- else
- -- You already owned it
- if player.EquippedTrail.Value ~= trail.Name then
- player.EquippedTrail.Value = trail.Name
- print(player.Name.." equipped the ".. trail.Name.." trail")
- return "Equip"
- else
- player.EquippedTrail.Value = ""
- print(player.Name.." unequipped the ".. trail.Name.." trail")
- return "Unequip"
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement