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

Why will the data store I made not save player data?

I am trying to make a player dataStore script, this has not been working for a while and now I am just now getting to it, can someone help?





local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	local money = Instance.new("IntValue",leaderstats)
	money.Name = "Money"
	money.Value = 0
	local MoneyData
	local success,err = pcall(function()
		MoneyData = MoneyDataStore:GetDataStore("plr: "..plr.UserId)
	end)
	if success then
		print("Loaded player data.")
		money.Value = MoneyData
	else
		print("Error loading data.")
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		MoneyDataStore:SetAsync("plr: "..plr.UserId,plr.leaderstats.Money.Value)
	end)
	if success then
		print("Saved player data.")
	else
		print("Error while saving player data.")
	end
end)




1 answer

You would need to do :GetAsync() and not GetDataStore when getting datastore's data

local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr --This is just more convenient
    local money = Instance.new("IntValue",leaderstats)
    money.Name = "Money"
    money.Value = 0
    local MoneyData
    local success,err = pcall(function()
        MoneyData = MoneyDataStore:GetAsync("plr: "..plr.UserId) --GetAsync and not GetDataStore
    end)
    if success then
        print("Loaded player data.")
        money.Value = MoneyData
    else
        print("Error loading data.") --Maybe change the Money value to 0 ?
    end
end)
game.Players.PlayerRemoving:Connect(function(plr)
    local success,err = pcall(function()
        MoneyDataStore:SetAsync("plr: "..plr.UserId,plr.leaderstats.Money.Value)
    end)
    if success then
        print("Saved player data.")
    else
        print("Error while saving player data.")
    end
end)




Also, another thing is that calling your key things like "plr: 9234231" is not real great and might mess up with other datastores. It would be more organised calling it something like "Money-242313". Other than that it looks all good.

Note: Next time make sure to add your output error to make it easier for other people to solve your issue and try to help you.

3 comments

0

there was no output error other than: 502: API Services rejected request with error. HTTP 403 (Forbidden). And it's still not working

0

Enable API Services in your game.

0

i did

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