Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why Is my script for data Store is not working I need someone to explain?

Here is my script.

--Locals
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

--Stats adder
game.Players.PlayerAdded:Connect(function(player)
	local leaderStats = Instance.new("Folder")
	leaderStats.Name = "leaderstats"
	leaderStats.Parent = player
	
	--Cash stats
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderStats
	-- Load Data
	
	local CashData
	
	
	local Success, errormessage pcall(function()
		CashData = MyDataStore:GetAsync(player.UserId.."-Cash")
	end)
	if Success then
		Cash.Value = CashData
		print("Data Loaded")
	else
		print("Data was NOT loaded :(")
		warn(errormessage)
	end
end)

--Saveing Stats
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("Data Saved")
	else
		print("Data was not saved :(")
		warn(errormessage)
	end
end)

1 comment

0

What is not working exactly?

2 answers

✓ Accepted answer

I have a solution if API services are disabled, turn that on. If that doesn't work then I have my own leaderboard that saves and is much smaller. Feel free to use it

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local cash = Instance.new("IntValue", folder)
	cash.Name = "Money"
	
	cash.Value = ds1:GetAsync(plr.UserId) or 500 --You can replace 500 with how much you want the starting money to be at
	ds1:SetAsync(plr.UserId, cash.Value)
	
	cash.Changed:connect(function()
		ds1:SetAsync(plr.UserId, cash.Value)
	end)
end)

I see you are doing this:

local Success, errormessage pcall(function()

But where is =?

Try this:

local Success, errormessage = pcall(function()

There also could be some stuff about it in the output, this is where you should look for issues.

2 comments

0

it just printing the errormessage in the out put

0

what does the error message say?

This discussion is preserved as it was. New answers, comments, and votes are closed.