DataStores Not Working

Hello, i am trying to make a basic DataStore script but it’s not working.
I tried to change Changed:Connect to PlayerRemoving but that too not worked.
Sorry for my bad english.
Here is the script;

[/quote]

1 Like

You shouldn’t save data when its changing, With exception of DataStore2 but you’re not using that here

Maybe use pcall(function()
They can tell you if there is an error and doesn’t stop processing the script midway.

I maked this but it didn’t work.

local DataStoreService = game:GetService(“DataStoreService”)
local CashSave = DataStoreService:GetDataStore(“SaveCash”)

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

local CashValue = CashSave:GetAsync(plr.UserId)
local Cash = Instance.new("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = CashValue

game.Players.PlayerRemoving:Connect(function()
local success, problem = pcall(function()
		CashSave:SetAsync(plr.UserId, Cash.Value)
	end)
	if success then
		print("Data Saved!")
	else
		print("An Error occured Data Saving Again")
		warn(problem)
	end
end)

end)

This post could help if the PlayerRemoving function doesn’t work.

I read but my problem is not this. My Data Store is not working and saving.

Where is your PlayerAdded and DataStore Service definitions?

Read carefully. I’m new and i don’t know how to make a script quote.

1 Like

Oh I know why! Wrong qutoation marks!
You should use these: “” not the weird ones you use on a phone.
“leaderstats” This line right here when I pasted into Studio it looks like those curly ones.

Uhmm. pretty sure that is the one u use in a phone (Discource converted em since its not in a code block)

Your problem is that PlayerRemoving isn’t working. This can solve your problem:

local DataStoreService = game:GetService("DataStoreService")
local CashSave = DataStoreService:GetDataStore("SaveCash")

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

local CashValue = CashSave:GetAsync(plr.UserId)
local Cash = Instance.new("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = CashValue
CashSave:SetAsync(plr.UserId, Cash.Value)

end)

game.Players.PlayerRemoving:Connect(function(plr)
CashSave:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
end)

game:BindToClose(function()
for _, plr in ipairs(game.Players:GetPlayers()) do
CashSave:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
end
end)

No i am not using phone, i was using “quote whole post” thanks.

Guys i am sorry if i was rude, i don’t know english very well.

Oh I know the problem now. You didn’t write what plr means, you only had it in PlayerAdded.

No they know what plr means cause there is a bracket in the end command

Can you try the script I sent you? @AlienX1258

Yeah, I reckon this is the simple problem, and you could easily fix it with:

local player = game.Players.PlayerAdded:connect(function(plr)

After the line:

local CashSave = DataStoreService:GetDataStore("SaveCash"

I edited the quotation marks and added the variable plr.

local DataStoreService = game:GetService("DataStoreService")
local CashSave = DataStoreService:GetDataStore("SaveCash")
local bruh
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"

local success, problem = pcall(function()
		local bruh = CashSave:GetAsync(plr.UserId)
	end)
	if success then
		local Cash = Instance.new("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = bruh
	else
local Cash = Instance.new("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = 0
		warn(problem)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, problem = pcall(function()
		CashSave:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
	end)
	if success then
		print("Data Saved!")
	else
		print("An Error occured Data Saving Again")
		warn(problem)
	end
end)

This works in my test place.

Oh i tried now and still not working. Making a currency but not loading or saving.
Although my api services is on.

No he only had it in PlayerAdded but not PlayerRemoving