My Data Store script doesn't work

So I had made a script that saved an intvalue value, but I wanted to add more than one value, but the way I had done it I couldn’t do it so I had to modify it. My problem is that after having modified it now it doesn’t work (there are no errors present in the output).

Here is the script:

local DataStoreService = game:GetService(“DataStoreService”)
local PlayerData = DataStoreService:GetDataStore(“PlayerDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local playerUserId = player.UserId
local data
local success, erro = pcall(function()
data = PlayerData:GetAsync(playerUserId)
end)
if success then
player.DataStore.Coin1.Value = data.Squiders
player.DataStore.Coins2.Value = data.Tixers
else
print(“error”)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local data = {
Coin1 = player.DataStore.Coin1.Value;
Coin2 = player.DataStore.Coin2.Value;
}
local sucess,erro = pcall(function()
PlayerData:SetAsync(playerUserId, data)
end)

if sucess then
	print("saved")
else
	print("not saved")
end

end)

What did I do wrong for the script not to work?

Get involved in one thing: player.DataStore.Coin1.Value = data.Squiders
player.DataStore.Coins2.Value = data.Tixers

but yes

player.DataStore.Coin1.Value = data.Coin1
player.DataStore.Coins2.Value = data.Coin2

Do you get “saved” or “not saved” in output?

Nothing appears in the output, I don’t understand why it doesn’t

seems like you are doing different variable names, at the PlayerAdded section you are getting data.Squiders and data.Tixers, while at the PlayerRemoving section you are saving these values as Coin1 and Coin2 not Squiders and Tixers
Try this:

local DataStoreService = game:GetService(“DataStoreService”)
local PlayerData = DataStoreService:GetDataStore(“PlayerDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local playerUserId = player.UserId
local data
local success, erro = pcall(function()
data = PlayerData:GetAsync(playerUserId)
end)
if success then
player.DataStore.Coin1.Value = data.Squiders
player.DataStore.Coins2.Value = data.Tixers
else
print(“error”)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local data = {}
data.Squiders = player.DataStore.Coin1.Value
data.Tixers = player.DataStore.Coin2.Value

local sucess,error = pcall(function()
PlayerData:SetAsync(playerUserId, data)
end)

if sucess then
	print("saved")
else
	print("not saved")
end
end)
1 Like

I guess when you SetAsync the table that you saved {Coin1 Coin2}. When you GetAsync what is data.Squiders and data.Tixers ?

I know I custom by generic names and I forgot to change it but I stated it above

updated script:
local DataStoreService = game:GetService(“DataStoreService”)

local PlayerData = DataStoreService:GetDataStore(“PlayerDataStore”)

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

local playerUserId = player.UserId

local data

local success, erro = pcall(function()

data = PlayerData:GetAsync(playerUserId)

end)

if success then

player.DataStore.Coin1.Value = data.Coin1

player.DataStore.Coins2.Value = data.Coin2

else

print(“error”)

end

end)

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

local playerUserId = player.UserId

local data = {}

data.Coin1 = player.DataStore.Coin1.Value

data.Coin2 = player.DataStore.Coin2.Value

local sucess,error = pcall(function()

PlayerData:SetAsync(playerUserId, data)

end)

if sucess then

print(“saved”)

else

print(“not saved”)

end

end)

Try to look at the currency name, sometimes you make the Big letter or Small letter wrong.

1 Like

Try adding a print("END OF CODE") at the end of PlayerRemoving and see if it even reaches the end of the code, try doing same thing to the PlayerAdding too

Also if you are testing in studio make sure studio has access to API services through game settings

1 Like

Did you enable API service? In Game Settings in studio > Security> Enable Studio Access to API

1 Like

The “end of the code” appears, so I noticed now, it appears that the data are saved but are not loaded but I activated the studio api

But how do you increase or decrease your currency
Maybe some way you change your currency will destroy the script

1 Like

Do you create the player.DataStore.Coin1 and the other variable BEFORE loading? maybe the script tries to load into these variables before they are even created, try inserting the script that creates these folders and values into this script, here is a tutorial on doing DataStores: https://www.youtube.com/watch?v=DkYupSBUpes

1 Like

My script was almost identical to the one in the video and it worked well with 1 value but I couldn’t make it work for 2 values.

But have you tried to make a script which is for Coin1 and make another scrript which is for Coin2
I just asking but idk will it work

1 Like

I know how to do that and I know it works just that I wanted to save space and learn new things.

Man, forget about current DataStore and go straight to DataStore2

DataStore2 is a bit complicated. Also it doesnt save much space actually

I’ve heard of it and I know the differences between normal but if there really isn’t any way to solve this, I have to choose this option.