I have made a money system in an obstacle course game, and I have obtained a script for buying trails in my game. I noticed that they have similar coding, which is why I've been attempting to combine it for the past two hours. However, I've been struggling to combine my two scripts and it's frustrating to no end. Can anyone help me out here?
First one
CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
function PlayerEntered(player)
repeat wait() until player.Character
local stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Parent = stats
cash.Name = "Gold"
if CashStore:GetAsync("Points_"..player.Name) ~= nil then
cash.Value = CashStore:GetAsync("Points_"..player.Name)
else
cash.Value = 0
end
cash.Changed:connect(function(Val)
CashStore:SetAsync("Points_"..player.Name, Val)
end)
end
game.Players.PlayerAdded:connect(PlayerEntered)
Second one
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("DATA")
local trails = game.ReplicatedStorage:WaitForChild("Trails")
function createAtchs(char)
local hrp = char:WaitForChild("HumanoidRootPart")
local atchTop = Instance.new("Attachment", hrp)
atchTop.Name = "TrailTop"
atchTop.Position = Vector3.new(0, 0.766, 0)
local atchBtm = Instance.new("Attachment", hrp)
atchBtm.Name = "TrailBottom"
atchBtm.Position = Vector3.new(0, -0.766, 0)
end
function saveData(plrLeaving)
local ownedTrails = {}
for i, trail in pairs(plrLeaving.OwnedTrails:GetChildren()) do
table.insert(ownedTrails, trail.Name)
end
local success, err = pcall(function()
ds:SetAsync("trails-" .. plrLeaving.UserId, ownedTrails)
CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
function PlayerEntered(player)
repeat wait() until player.Character
local stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Parent = stats
cash.Name = "Gold"
if CashStore:GetAsync("Points_"..player.Name) ~= nil then
cash.Value = CashStore:GetAsync("Points_"..player.Name)
else
cash.Value = 0
end
cash.Changed:connect(function(Val)
CashStore:SetAsync("Points_"..player.Name, Val)
end)
end
game.Players.PlayerAdded:connect(PlayerEntered)
game.Players.PlayerAdded:Connect(function(plr)
local trailsOwned = {}
local cash = 2000
pcall(function()
trailsOwned = ds:GetAsync("trails-" .. plr.UserId) or {}
cash = ds:GetAsync("Points_" .. plr.UserId) or 2000
end)
local ls = Instance.new("Folder", plr)
ls.Name = "leaderstats"
local cash = Instance.new("IntValue", ls)
cash.Name = "Gold"
cash.Value = cash
local ownedFolder = Instance.new("Folder", plr)
ownedFolder.Name = "OwnedTrails"
for i, owned in pairs(trailsOwned) do
if trails:FindFirstChild(owned) then
trails[owned]:Clone().Parent = ownedFolder
end
end
if plr.Character then createAtchs(plr.Character) end
plr.CharacterAdded:Connect(createAtchs)
end)
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for i, plrLeaving in pairs(game.Players:GetPlayers()) do
saveData(plrLeaving)
end
end)
game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, trail)
if buying and not plr.OwnedTrails:FindFirstChild(trail.Name) then
local price = trail.Price.Value
local cash = plr.leaderstats.Gold
if price <= cash.Value then
cash.Value -= price
trail:Clone().Parent = plr.OwnedTrails
end
elseif not buying and plr.OwnedTrails:FindFirstChild(trail.Name) then
local char = plr.Character
if not char or not char:FindFirstChild("HumanoidRootPart") then return end
for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
if child:IsA("Trail") then child:Destroy() end
end
local newTrail = trail:Clone()
newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
newTrail.Parent = char.HumanoidRootPart
end
end)
Much appreciated if anyone took a look at this, or attempted to merge it. :)

2 comments
What's the error? Explain it in your question.
The error is that it's not allowing me to have the gold visible in the game. It'll pop up as "N/A" if I have both of these scripts ongoing.