Datastore not working

I have been trying to use datastores for a while but haven’t been able to get them to work.

I am getting this error also: Http Request failed: HTTP 429 (Too Many Requests)

heres the datastore script I am using:

local DataStoreService = game:GetService(“DataStoreService”)

local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats

local data
local success, errormessage = pcall(function()
	myDataStore:GetAsync(player.UserId.."-cash")
end)

if success then
	money.Value = data
else
	print("There was an error whilst getting your data")
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)

if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving data")
	warn(errormessage)
end

end)

1 Like

when you are testing for Datastore, you can simplify your approach.
You don’t necessary need the whole leaderstats routine.
You can simply use a single value.

In your case, when you loaded in the data

myDataStore:GetAsync(player.UserId.."-cash")

The data here is falling into the void. You need to capture it with a variable data.

Is it not working in studio or both ingame and studio?

since you haven’t stored anything in data variable its not gonna work.

heres the working script:

local DataStoreService = game:GetService(“DataStoreService”)

local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."-cash")
end)

if success then
	money.Value = data
else
	print("There was an error whilst getting your data")
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)

if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving data")
	warn(errormessage)
end

end)

Does it make sense to you now? You rename the integer value as Money and you try to get it as Cash. Change Cash to Money.

Also, make sure to publish your game since datastores won’t be open unless the game is published.

1 Like
local DataStoreService = game:GetService(“DataStoreService”)

local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats

local data
local success, errormessage = pcall(function()
	local data=myDataStore:GetAsync(player.UserId.."-money")
end)

if success then
	money.Value = data
else
	print("There was an error whilst getting your data")
	warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Money.Value)
end)

if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving data")
	warn(errormessage)
end
end)

try this

on line 11 you typed:

money.Name = "Money"

and on line 30 then you typed

myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)

maybe try changing it with this:

myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Money.Value)

It seems like you’re not getting the data.
You don’t even need the data variable, here’s a better method:

local Ok, Result = pcall(function()
   return Key:GetDataStore(PlayerKey)
end)
if Ok then
   Money.Value = Result or 0 -- 0 in case the result is nil, as players that don't have any data will be nil.
end

I tried that and got an error saying: Http Request failed: HTTP 429 (Too Many Requests)

It says unknown global for key and playerkey

Try this:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."-cash")
end)

if success then
	money.Value = data
else
	print("There was an error whilst getting your data")
	warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)

if success then
	print("Player Data successfully saved!")
else
	print("There was an error when saving data")
	warn(errormessage)
end
end)

do you have other scripts that contains saving if yes pls show it

Because that’s not the whole script.
You need to script it yourself, we cannot provide you with fully-working code as it is against the forum rules.

I have other scripts that change a leaderstat value but not that contains saving

I think the problem is the datastore works I’m just not displaying the data properly in-game

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.