Data store won't set multiplier!

Hi there! I’m making a simulator game but I have a problem. My multiplier is acting strangely. Here is the data store script I applied:

local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

local function onServerShutdown()
	
	for _, player in pairs(game.Players:GetPlayers()) do
		
		local success, errmsg = pcall(function()
			dataStore:SetAsync(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
		end)

		if success then
			print("Successfully saved your data!")
		else
			print("Unable to save data: "..errmsg)
			warn(errmsg)
		end
		
	end
	
end

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local protect = Instance.new("IntValue")
	protect.Name = "Protection"
	protect.Parent = leaderstats
	protect.Value = 0
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	rebirths.Value = 0
	
	local multiplier = Instance.new("NumberValue")
	multiplier.Name = "Multiplier"
	multiplier.Parent = player
	multiplier.Value = 1
	
	rebirths.Changed:Connect(function()
		if rebirths.Value + 1 then
			multiplier.Value += 1
		end
	end)
	
	local data
	
	local success, errmsg = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	
	if not success then
		player:Kick("Fetch Data Error: "..errmsg..". Therefore we had to kick you to prevent data loss. Sorry! :(")
		warn(errmsg)
	end
	
	if success then
		if data then
			protect.Value = data[1]
			rebirths.Value = data[2]
			multiplier.Value = data[3]
			print("Welcome back "..player.Name.."! You had "..protect.Value.." protection since last play.")
		else
			print("This is a new player or they just have no data.")
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errmsg = pcall(function()
		dataStore:SetAsync(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
	end)
	
	if success then
		print("Successfully saved your data!")
	else
		print("Unable to save data: "..errmsg)
		warn(errmsg)
	end
end)

game:BindToClose(onServerShutdown)

The multiplier is supposed to start at 1 but it keeps setting at 0 (Even when I switch it to leaderstats). Is there a way to fix this issue? All suggestions are appreciated!

1 Like

I tested your script and it worked completely fine for me, could another script you have be changing the value?

That’s the only script I have in ServerScriptService.

Do you have other scripts in other places? Your code worked completely fine for me.

Do you have datastore api on??

I might has thought of the problem, the value of it may allready be 0 in the datastore so it sets it to that. You could change the data store you are using to test that or check it with the legendary DataStore Editor plugin.

Command bar option

game:GetService("DataStoreService"):GetDataStore("PlayerData"):UpdateAsync(game.CreatorId, function(data) data[3] = nil; return data end)

his code doesn’t have compatability if a value is nil, it would all break. he should just do instead

game:GetService("DataStoreService"):GetDataStore("PlayerData"):SetAsync(game.CreatorId, nil)
1 Like

Yes I do. (30-Character limit)

I actually had a module script for it before I written this one. Sadly, it didn’t work either.

local DSS = game:GetService("DataStoreService")

local dataStore = DSS:GetDataStore("PlayerData")

local dataStoreSettings = {}

function dataStoreSettings:GetKeyData(key)
	return dataStore:GetAsync(key)
end

function dataStoreSettings:SetDataToTable(key, tableToSave)
	dataStore:SetAsync(key, tableToSave)
end

return dataStoreSettings
local dataStoreSettings = require(script.DataStoreModules.MainDataFunctions)

local function onServerShutdown()
	
	for _, player in pairs(game.Players:GetPlayers()) do
		
		local success, errmsg = pcall(function()
			dataStoreSettings:SetDataToTable(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
		end)

		if success then
			print("Successfully saved your data!")
		else
			print("Unable to save data: "..errmsg)
			warn(errmsg)
		end
		
	end
	
end

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local protect = Instance.new("IntValue")
	protect.Name = "Protection"
	protect.Parent = leaderstats
	protect.Value = 0
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	rebirths.Value = 0
	
	local multiplier = Instance.new("NumberValue")
	multiplier.Name = "Multiplier"
	multiplier.Parent = player
	multiplier.Value = 1
	
	rebirths.Changed:Connect(function()
		if rebirths.Value + 1 then
			multiplier.Value += 1
		end
	end)
	
	local data
	
	local success, errmsg = pcall(function()
		data = dataStoreSettings:GetKeyData(player.UserId)
	end)
	
	if not success then
		player:Kick("Fetch Data Error: "..errmsg..". Therefore we had to kick you to prevent data loss. Sorry! :(")
		warn(errmsg)
	end
	
	if success then
		if data then
			protect.Value = data[1]
			rebirths.Value = data[2]
			multiplier.Value = data[3]
			print("Welcome back "..player.Name.."! You had "..protect.Value.." protection since last play.")
		else
			print("This is a new player or they just have no data.")
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errmsg = pcall(function()
		dataStoreSettings:SetDataToTable(player.UserId, {player.leaderstats.Protection.Value, player.leaderstats.Rebirths.Value, player.Multiplier.Value})
	end)
	
	if success then
		print("Successfully saved your data!")
	else
		print("Unable to save data: "..errmsg)
		warn(errmsg)
	end
end)

game:BindToClose(onServerShutdown)

By the way, I find it strange because the rebirth and other value works perfectly.

Just tested your code and it works for me.
Just to test Run the game press F9 click on server and type this statement

game.Players.LiljayGamer_X.Multplier.Value = 30

Close the game and Run again and check if the value is 30

1 Like

Thanks @S3nt1ne3l but I just found out what I had to do: it turns out that this line was messing it all up:

multiplier.Value = data[3]

Sorry if I was wasting your time.

1 Like