Hello! I am trying to create a script that creates and saves a bunch of leaderstats. The creation part works. However, the saving part of said script isn’t working. I have been getting an error at line 44 saying “ServerScriptService.Leaderstats script:44: attempt to index number with ‘Points’” Scanning the script, I can’t seem to find any issues. Any help?
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Currency = Instance.new("IntValue", Leaderstats)
Currency.Name = "Points"
Currency.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Currency.Value = Data
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointStats") -- Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Points= Instance.new("IntValue", Leaderstats)
Points.Name = "Points"
Points.Value = 0
local Kills= Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Timealive= Instance.new("IntValue", Leaderstats)
Timealive.Name = "Timealive"
Timealive.Value = 0
local BestTime= Instance.new("IntValue", Leaderstats)
BestTime.Name = "BestTime"
BestTime.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Points.Value = Data.Points
Kills.Value = Data.Kills
Timealive.Value = Data.Timealive
BestTime.Value = Data.BestTime
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Money"] = Player.leaderstats.Points.Value;
["Kills"] = Player.leaderstats.Kills.Value;
["Timealive"] = Player.leaderstats.TimeAlive.Value;
["BestTime"] = Player.leaderstats.BestTime.Value;
})
end)
I changed that, and it looks like it still has the same error. Just to confirm, but is it supposed to be a script in ServerScriptStorage? (I spent way too long one time trying to find an error for it to be in the wrong area)
You’re setting the datastore twice, with 2 differrent data points.
Try removing
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Currency = Instance.new("IntValue", Leaderstats)
Currency.Name = "Points"
Currency.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Currency.Value = Data
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)
from the script, and it may result in the desired outcome.
i.e.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointStats") -- Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Points= Instance.new("IntValue", Leaderstats)
Points.Name = "Points"
Points.Value = 0
local Kills= Instance.new("IntValue", Leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
local Timealive= Instance.new("IntValue", Leaderstats)
Timealive.Name = "Timealive"
Timealive.Value = 0
local BestTime= Instance.new("IntValue", Leaderstats)
BestTime.Name = "BestTime"
BestTime.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Points.Value = Data.Points
Kills.Value = Data.Kills
Timealive.Value = Data.Timealive
BestTime.Value = Data.BestTime
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["Points"] = Player.leaderstats.Points.Value;
["Kills"] = Player.leaderstats.Kills.Value;
["Timealive"] = Player.leaderstats.TimeAlive.Value;
["BestTime"] = Player.leaderstats.BestTime.Value;
})
end)
Alright, so tested it by going onto the game’s site and it does look like what I think you thought it was. Roblox Studio decided to not save the data but it seems to be working on the game itself.