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

Why doesnt my script save any data? It prints to console a success message but doesnt work.

this is the code in question from the title. When i run the script i get the success message printed to console when the data is saved however, ive checked with a datastore editor and this isnt the case. Ive spent 3 days trying to get this to save stuff but it just doesnt

local DataStoreService = game:GetService("DataStoreService")
local gameDataStore = DataStoreService:GetDataStore("gameDataStore")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
	player = player
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue",leaderstats)
	money.Name = "money"
	money.Value = 10
	
	local strength = Instance.new("IntValue",leaderstats)
	strength.Name = "strength"
	strength.Value = 0
	
	local newplayer = Instance.new("IntValue", leaderstats)
	newplayer.Name = "newplayer"
	newplayer.Value = 1
	-- load data
	local success, errormessage = pcall(function()
		dataDict = gameDataStore:GetAsync(player.UserId)
	end)
	
	if success then
		player.leaderstats.money.Value = dataDict.moneydata
		player.leaderstats.strength.Value = dataDict.strengthdata
		player.leaderstats.newplayer.Value = dataDict.newplayerdata
		print("loaded data")
	else
		print("big homosexual while getting data")
		warn(errormessage)
	end

end)
-- The part below this does the saving
local function gamesave(player)
	print(player)
	local UserId = player.UserId
	
	local money = player.leaderstats.money.Value
	
	local strength = player.leaderstats.strength.Value
	
	local newplayer = player.leaderstats.newplayer.Value
	
	local dataDict = {
		moneydata = money,
		strengthdata = strength,
		newplayerdata = newplayer
	}

	
	local success, errormessage = pcall(function()
		gameDataStore:SetAsync(UserId,dataDict)
	end)

	if success then
		print("data saved")
	else
		print("data failed")
		warn(errormessage)
	end
end
game.Players.PlayerRemoving:Connect(function(player)
	for i, player in pairs(Players:GetPlayers()) do
		if i > 1 then
			print(i)
			gamesave(player)
		end
	end	
end)

game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do
		if i == 1 then
			print(i)
			gamesave(player)
			break
		end
		
	end
end)


2 comments

0

i should also mention that i get this message in console

0

Are you completely sure that it is not your loading script flawed?

1 answer

it is that for loop in BindToClose, i wont be 1, i is the players service, same with playerremoving, i wont be a number, i is the player service

1 comment

0

you actually dont even need the if statements in the for loops, it wont help anything. Also dont loop through the players when someone leave, just do gamesave(player) to save that very player's data

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