I was working on a checkpoints script where I had saving checkpoints, and I wanted to make the game more fun, so I decided to add a money system. The money works great except for one issue: it doesn’t save to the datastore…
No matter what I tried the money wouldn’t save to the datastore. and I was starting so wonder what I was doing wrong.
So I tried many other ways like copying and pasting the same datastore stuff I used for the checkpoints (I am not a very good scripter) Or even putting the money value inside of another script; but still no results. I’m sure its possible but I’m not sure what else I can do to get the money to save at this point.
I know it is a long script, but if there is a way someone can find a way to get the money value to save that would be great! The person who did the tutorial made this in an interesting way, like a script I’ve never seen before so that made it a little harder for me.
Here is the script that I was working on, I’m not sure if you need the whole thing, but here it is just in case you need it because the datastore parts of it are all over the script.
local Players = game:GetService("Players")
local DataStoreStore = game:GetService("DataStoreService")
local ObbyDataStore = DataStoreStore:GetDataStore("ObbyDataStore")
local Checkpoints = workspace:WaitForChild("Checkpoints")
local inGameStartupPlayers = {}
local CurrentStage = {}
local TouchDb = {}
local function NewCharacter (player, char)
local TempCurrentStage = CurrentStage[player.UserId]
if TempCurrentStage ~= nil then
local TempCheckpoint = Checkpoints:FindFirstChild(TempCurrentStage)
if TempCheckpoint ~= nil then
repeat wait(0.1) until char.PrimaryPart ~= nil
char:SetPrimaryPartCFrame(CFrame.new(TempCheckpoint.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(TempCheckpoint.Orientation.Y) + math.rad(90), 0))
end
end
end
local function NewPlayer(player)
local success, stage = pcall(function()
return(ObbyDataStore:GetAsync(player.UserId)) or 1
end)
CurrentStage[player.UserId] = stage
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"
Stage.Value = stage
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money"
Money.Value = 0
local TempChar = player.Character
if TempChar ~= nil then
NewCharacter(player, TempChar)
end
player.CharacterAdded:Connect(function(char)
NewCharacter(player, char)
end)
end
Players.PlayerAdded:Connect(function(player)
if inGameStartupPlayers[player] == nil then
NewPlayer(player)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success = pcall(function()
ObbyDataStore:SetAsync(player.UserId, CurrentStage[player.UserId])
end)
CurrentStage[player.UserId] = nil
end)
for i,v in pairs(Checkpoints:GetChildren()) do
local StageNum = tonumber(v.Name)
v.Touched:Connect(function(hut) --Remember to Change this to hut instead of hit so we dont have to many hit functions!--
local char = hut.Parent --Change this to hut as well to match the other when I insert this into the other game
if char ~= nil then
local Humanoid = char:FindFirstChildOfClass("Humanoid")
if Humanoid ~= nil and Humanoid.Health > 0 then
local player = Players:GetPlayerFromCharacter(char)
if player ~= nil and (TouchDb[player.UserId] or 0) + 1 <= os.time() then
TouchDb[player.UserId] = os.time()
local TempCurrentStage = CurrentStage[player.UserId]
if TempCurrentStage == StageNum - 1 then
CurrentStage[player.UserId] = StageNum
local TempLeaderstats = player:FindFirstChild("leaderstats")
if TempLeaderstats ~= nil then
local TempStage = TempLeaderstats:FindFirstChild("Stage")
if TempStage ~= nil then
TempStage.Value = StageNum
end
end
end
end
end
end
end)
end
inGameStartupPlayers = Players:GetPlayers()
for i,v in pairs(inGameStartupPlayers) do
spawn(function()
NewPlayer(v)
end)
end
game:BindToClose(function()
for i,v in pairs(Players:GetPlayers()) do
ObbyDataStore:SetAsync(v.UserId, CurrentStage[v.UserId])
end
wait(1)
end)
inGameStartupPlayers = {}