Why am I getting this error for my data saving leaderstats?

This is the error: ServerScriptService.Scripts.Stats.leaderstats:31: attempt to index number with ‘Power’

So the error is at line 31, the code works whenever I leave the game it saves my data (sometimes for some reason) but I don’t know why I get this erro??

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

game.Players.PlayerAdded:Connect(function(player)

	local Cashid = player.UserId.."_Cash"
	local Powerid = player.UserId.."_Power"
	local Zoneid = player.UserId.."_Zone"
	
	local CashData = ds:GetAsync(Cashid)
	local PowerData = ds:GetAsync(Powerid)
	local ZoneData = ds:GetAsync(Zoneid)


	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	
	local Power = Instance.new("IntValue",leader)
	Power.Name = "Power 💪"
	Power.Value = PowerData or 0

	local Cash = Instance.new("IntValue",leader)
	Cash.Name = "Cash 💰"
	Cash.Value = CashData or 0
	
	local Zone = Instance.new("IntValue",leader)
	Zone.Name = "Zone 🗺️"
	Zone.Value = ZoneData or 1

	if PowerData then
	--Error is here
	   Power.Value = PowerData["Power 💪"]
	end
	
	if CashData then
		Cash.Value = CashData["Cash 💰"]
	end
	
	if ZoneData then
		Zone.Value = ZoneData["Zone 🗺️"]
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Cashid = player.UserId.."_Cash"
	local Powerid = player.UserId.."_Power"
	local Zoneid = player.UserId.."_Zone"

	ds:SetAsync(Powerid, player.leaderstats["Power 💪"].Value)
	ds:SetAsync(Cashid, player.leaderstats["Cash 💰"].Value)
	ds:SetAsync(Zoneid, player.leaderstats["Zone 🗺️"].Value)
end)

PowerData is not a table. It’s a number. It’s a bit confusing - what is meant to be what?

So basically before using this code I was using this:

Power.Changed:Connect(function()
     ds:SetAsync(Powerid, Power.Value)
end)

Which was working but I was getting an error because the data requests were too many ( I was incrementing the power every second by 1), so now I tried to change to this system which I don’t really understand but I saw a tutorial on it but it doesn’t seem to be working

The reason why you got too many data requests is because you were saving too frequently. Reduce the autosave amount and don’t save after every change.

Then when should I save? I tried removing it and making it save only when the player gets removed, but it didn’t work

I’d save every 10 minutes or so. Or, you can do a forced save whenever they do something game-changingly important.
PlayerRemoving works quite strangely in studio to my knowledge. I’d suggest also saving on game:BindToClose as well.

PowerData is a number, not a table. Therefore the solution would be this:
Power.Value = PowerData

1 Like

try this:
local DataStore = game:GetService(“DataStoreService”)
local ds = DataStore:GetDataStore(“LeaderStatSave”)

game.Players.PlayerAdded:Connect(function(player)

local Cashid = player.UserId.."_Cash"
local Powerid = player.UserId.."_Power"
local Zoneid = player.UserId.."_Zone"

local CashData = ds:GetAsync(Cashid)
local PowerData = ds:GetAsync(Powerid)
local ZoneData = ds:GetAsync(Zoneid)


local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"

local Power = Instance.new("IntValue",leader)
Power.Name = "Power 💪"
Power.Value = PowerData or 0

local Cash = Instance.new("IntValue",leader)
Cash.Name = "Cash 💰"
Cash.Value = CashData or 0

local Zone = Instance.new("IntValue",leader)
Zone.Name = "Zone 🗺️"
Zone.Value = ZoneData or 1

if PowerData then
	--Error is here
	Power.Value = PowerData or 0
end

if CashData then
	Cash.Value = CashData or 0
end

if ZoneData then
	Zone.Value = ZoneData or 0
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local Cashid = player.UserId…“_Cash”
local Powerid = player.UserId…“_Power”
local Zoneid = player.UserId…“_Zone”

ds:SetAsync(Powerid, player.leaderstats["Power 💪"].Value)
ds:SetAsync(Cashid, player.leaderstats["Cash 💰"].Value)
ds:SetAsync(Zoneid, player.leaderstats["Zone 🗺️"].Value)

end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.