Datastore problem

So basically i don’t get any errors when leaving my game and my cash doesnt save

This is the script

> local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
> game.Players.PlayerAdded:Connect(function(player)
> 	wait()
> 	local playerId = "id_"..player.UserId
> 	local save1 = player.leaderstats.Cash
> 	
> 	
> 	local GetSaved = dataStore:GetAsync(playerId)
> 	if GetSaved then 
> 		save1.Value = GetSaved[1]
> 	else
> 		local NumberForSaving = {save1.Value}
> 		dataStore:GetAsync(playerId,NumberForSaving)
> 	end
> end)
> 
> game.Players.PlayerRemoving:Connect(function(player)
> 	dataStore:SetAsync("id_"..player.userId, {player.leaderstats.Cash.Value})
> end)

Also i will be extremely happy if you can tell me how can i actually save what i have bought or equipped, this is a phone game…

In the PlayerRemoving event, you have a typo
In the else block, instead of using dataStore:GetAsync , you should use dataStore:SetAsync to save the player’s data:

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(player)
    wait()
    local playerId = "id_"..player.UserId
    local save1 = player.leaderstats.Cash

    local GetSaved = dataStore:GetAsync(playerId)
    if GetSaved then 
        save1.Value = GetSaved[1]
    else
        local NumberForSaving = {save1.Value}
        dataStore:SetAsync(playerId, NumberForSaving) -- Fixed typo here
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    dataStore:SetAsync("id_"..player.userId, {player.leaderstats.Cash.Value})
end)

If this doesn’t solve the issue, make sure that the DataStore is enabled in the game’s configuration page, and that the data store key "SaveData" is spelled correctly.

1 Like

you need to WaitForChild(“leaderstats”) and then WaitForChild(“Cash”) when PlayerAdded

1 Like

There is quite a bit wrong with this script, but I don’t have much time to explain everything. I have to go in a few minutes, however, I do have a large post on DataStores, and maybe something in it can help.

It might be because you aren’t using pcalls (which you really should), or because the server closes before it saves. You should always add a game:BindToClose() when using DataStores. That’s just what I think could be the issue, though. Here is the big post:

1 Like

I tried inserting this script and it still doesn’t save, do i need to also send the leaderstats script? Maybe there is a problem but it certain not be.

yes,have you also checked if DataStore is enabled in the game’s configuration?

1 Like

I have checked the API settings already.

This is the code for the phone that gives you cash

local tool = script.Parent
local Player = tool.Parent.Parent
local Cash = Player.leaderstats.Cash
local Value = 2
local GamepassID = 146320044

script.Parent.Activated:Connect(function(player)
	if not script.Parent.Ringtone.IsPlaying then
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, GamepassID) then
		Cash.Value = Cash.Value + Value * 2
		else
		Cash.Value = Cash.Value + Value
		end
		script.Parent.Ringtone:Play()
		
	else
	--nothing lol
	end
	
end)

and this is the leaderstats script.

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

The script looks fine and should work correctly. However, there is one potential issue you may want to consider.

The code assumes that script.Parent refers to a Tool object. If the script is placed in a different object (e.g. a part), then script.Parent will refer to that object instead and the script may not work as intended. It’s a good practice to explicitly set the tool variable to the Tool object instead of relying on script.Parent .

Other than that, the script should work as intended

1 Like
else
> 		local NumberForSaving = {save1.Value}
> 		dataStore:GetAsync(playerId,NumberForSaving)

You’re using GetAsync here, you should be using SetAsync as GetAsync only takes in one parameter and needs a variable to set it to.

1 Like

Yea, the game is like a clicker game and i use a tool
image

Do you think there is a fix or should i recode it and see if it works?

I can give you code by person called @TheDevKing I always use it to save data

local DataStoreService = game:GetService("DataStoreService")
local PlayersService = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("DataStore")

PlayersService.PlayerAdded:Connect(function(Player)
	
	Player:WaitForChild("leaderstats")
	local leaderstats = Player:FindFirstChild("leaderstats")
	
	leaderstats:WaitForChild("Cash")
	local Cash = leaderstats:FindFirstChild("Cash")

	local PlayerUserId = "Player_"..Player.UserId
	
	--Load Data--
	
	local Data
	local success, erromessage = pcall(function()
		Data = DataStore:GetAsync(PlayerUserId)
	end)
	
	if success == true then
		if Data then
		Cash.Value = Data.Cash
			end
	end
		
end)

PlayersService.PlayerRemoving:Connect(function(Player)
	local PlayerUserId = "Player_"..Player.UserId
	
	local Data = {
		Cash = Player.leaderstats.Cash.Value
	}
	local success, errormessage = pcall(function()
	DataStore:SetAsync(PlayerUserId, Data)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)

Also check if you have in settings in security Enable Studio Access to API Services Enabled

Try to use function instead local function in leaderstats script

I tried the script that you sent me and it’s still not saving anything… I am so confused and i also enabled API Services.

also
Screenshot 2023-03-10 141615

Try to use this script for leaderstats if it won’t save then tell me pls is script for phone is local script?
Script for leaderstats:

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

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = leaderstats
end)
1 Like

lol im stupid, the entire time the phone had a local script🤦‍♂️ Anyways, thanks for telling me to check the script

1 Like

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