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)
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
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.
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”