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

How can I fix my data store, it wont work and dosen't leave a error?

After putting in the 2nd value (Bits) this data store just stopped working.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("WinsBits")


game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Bits = Instance.new("IntValue")
	Bits.Name = "Bits"
	Bits.Parent = leaderstats
	
	local playerUserId = "player_"..player.UserId
	
	local data 
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
		Wins.Value = data.Wins
		Bits.Value = data.Bits
		end
		end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "player_"..player.UserId
	
	local data = {
		Wins = player.leaderstats.Wins.Value; 
		Bits = player.leaderstats.Bits.Value
	}
	
	
	local success, errormessage = pcall(function()
	DataStore:SetAsync(playerUserId, data)
	end)
	
	if success then 
		print("Data successfully saved!")
	else
		print("error occured")
		warn(errormessage)
	end
end)

Thanks in advance

1 comment

0

make sure you ether test this in a game, not roblox studio, or a team test

1 answer

You need to use a BindToClose function. The error you are having is most likely that the PlayerRemoving is never getting fired.

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