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

Why isn't this script saving the leader board data?

I have enabled studio access to API services. I also have this script in the server script service to save my coins. But it doesn't. What's wrong?

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


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	-- Data Store Section
	
	local playerUserId = "Player_"..player.UserId
	print(playerUserId)
	
	-- Load Data
	
	local data
	local success, errormessage = pcall(function()
		 data = myDataStore:GetAsync(playerUserId)
	
	end)
		
		coins.Value = data
		
		
	if success then
		coins.Value = data 
		-- Set our data equal to the current Coins
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = player.leaderstats.Coins.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
		if success then
			print("Data successfully saved!")
	    else
			print("There was an error saving the data!")
			warn(errormessage)	
		end
	
end)

1 comment

0

Does it print "Data successfully saved!" or "There was an error saving the data!" when you leave?

1 answer

No idea if it works though,

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinsSaveSystem")

game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"
    local Coins = Instance.new("IntValue",leader)
    Coins.Name = "Coins"
    Coins.Value = ds:GetAsync(player.UserId) or 0
    ds:SetAsync(player.UserId, Coins.Value)
    Coins.Changed:connect(function()
        ds:SetAsync(player.UserId, Coins.Value)
    end)
end)


game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player,leaderstats.Coins.Value)
end)


1 comment

0

make it a script and put it in ServerScriptService

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