Why won't the leaderstat change value when it's supposed to, and it doesn't save?

I started over with my leaderstat script to make it save, but it doesn’t save or change in value. I am getting the error 10:04:04.915 - 502: API Services rejected request with error. HTTP 403 (Forbidden)
Script for Leaderstat:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinsSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Gold = Instance.new("IntValue",leader)
 Gold.Name = "Coins"
 Gold.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, Gold.Value)
 Gold.Changed:connect(function()
  ds:SetAsync(player.UserId, Gold.Value)
 end)
end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)

Script for the objects giving money:

function CoalOre()
	local Ore = Instance.new("Part",workspace)
	Ore.Anchored = true
	Ore.CFrame = CFrame.new(math.random(-50, 50), 1, math.random(-50, 50))
	Ore.Shape = Enum.PartType.Ball
	Ore.TopSurface = Enum.SurfaceType.Smooth
	Ore.BottomSurface= Enum.SurfaceType.Smooth
	Ore.Size = Vector3.new(3, 3, 3)
	Ore.BrickColor = BrickColor.new("Black")
	Ore.Material= Enum.Material.Pebble
	Ore.Touched:connect(function(p)
		if p.Parent.Parent.Name == "Coal Pickaxe" or p.Parent.Parent.Name == "Copper Pickaxe" or p.Parent.Parent.Name == "Bronze Pickaxe" or p.Parent.Parent.Name == "Iron Pickaxe" or p.Parent.Parent.Name == "FireStone Pickaxe" or p.Parent.Parent.Name == "IceCrystal Pickaxe" then
			Ore:Destroy()
			local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
			local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			local leaderstats = player:FindFirstChild("leaderstats")
			local Coins = leaderstats:FindFirstChild("Gold")
			if Coins then Coins.Value = Coins.Value + 2 
		  end
		end
	end)
end
while wait(math.random(2,5)) do
	CoalOre()
end

I experienced that error when I tried making a game on another account with an another account which is logged in in studio

1 Like

You need to enable API access from the Game page on Roblox, or from Game Settings in studio.

From Roblox

Go to Create
image
Go to Configure Game
image
Ensure this is ticked:
image

From Studio

Go to Home > Game Settings


Then go to Options, and enable it from there.

1 Like

You likely haven’t enabled API access to services. Go to game settings > options, the setting will be there.

Here:
image

Make sure this checkbox is checked.

1 Like

You still can save data on studio using DataStore`s

I did that, and now I don’t get that error, but it still won’t save or change in value

Its really easy to check that you are using another account or else it’s Api services

1 Like

Your issue is that PlayerRemoving doesn’t fire in local studio test (because the server closes before it can actually fire it). What you should be doing is using game:BindToClose() which will run a function before the server itself closes. One of the most common usecases is to save all user data before closing the server

game:BindToClose(function()
   for _, v in pairs(Players:GetPlayers()) do
      pcall(saveData, v)
   end
end)

ps Roblox recommend having a check to make sure it’s not studio (RunService:IsStudio) otherwise you’ll have to wait for BindToClose() to finish before returning to edit mode

Get it from a string instead

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinsSaveSystem")

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
 local Gold = Instance.new("IntValue",leader)
 Gold.Name = "Coins"
 Gold.Value = ds:GetAsync("Gold"..player.UserId) or 0

end)


game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)

DataStore`s are not save proof

You can just use DataStore2 instead

1 Like

Please avoid using DS2, it adds unnecessary redundancies to your code, as well not being able to use UpdateAsync, a fairly useful function inside vanilla datastores which allows you to update your values without losing the original value

2 Likes

There’s no way for me to check if this would actually work, because, the Gold/Coins value won’t change with this:

function CoalOre()
	local Ore = Instance.new("Part",workspace)
	Ore.Anchored = true
	Ore.CFrame = CFrame.new(math.random(-50, 50), 1, math.random(-50, 50))
	Ore.Shape = Enum.PartType.Ball
	Ore.TopSurface = Enum.SurfaceType.Smooth
	Ore.BottomSurface= Enum.SurfaceType.Smooth
	Ore.Size = Vector3.new(3, 3, 3)
	Ore.BrickColor = BrickColor.new("Black")
	Ore.Material= Enum.Material.Pebble
	Ore.Touched:connect(function(p)
		if p.Parent.Parent.Name == "Coal Pickaxe" or p.Parent.Parent.Name == "Copper Pickaxe" or p.Parent.Parent.Name == "Bronze Pickaxe" or p.Parent.Parent.Name == "Iron Pickaxe" or p.Parent.Parent.Name == "FireStone Pickaxe" or p.Parent.Parent.Name == "IceCrystal Pickaxe" then
			Ore:Destroy()
			local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
			local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			local leaderstats = player:FindFirstChild("leaderstats")
			local Coins = leaderstats:FindFirstChild("Gold")
			if Coins then Coins.Value = Coins.Value + 2 
		  end
		end
	end)
end
while wait(math.random(2,5)) do
	CoalOre()
end

Add an end after the number 2 that may be why unless u have putten an end below it

DataStore2 has a time stamp where if you save data it will save that stamp so if the player`s data is lost it will make it revert to the previous time stamp

Just saying

That script was working back when I had the leaderstat script as

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	local Money = Instance.new("IntValue")
	Money.Name = "Gold"
	Money.Value = 0
	Money.Parent = leaderstats
end
 
game.Players.PlayerAdded:Connect(onPlayerJoin)

Then change it to that I don’t seem why changing it

I was trying to make the leaderstat save when the player joins the game again so they don’t have tostart all over

Dont just wrap the data store stuff in a pcall so it won’t Break

1 Like

WouldI be ableto do

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	local Money = Instance.new("IntValue")
	Money.Name = "Gold"
	Money.Value = 0
	Money.Parent = leaderstats
end
local function onPlayerLeave(player)
	Money.Value = player.Money
end
 
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerLeave)

Instead of keeping this topic on going just search for a tutorial on Roblox Studio How To Use DataStores

2 Likes