When I join I get the error message "Script:48: attempt to index nil with ‘Points’ " ( At playerjoined when the variables are assigned their values in the for loop)
I can use the values ingame correctly, but for some reason it won’t get saved/loaded as they just reset to 0 when rejoining.
Does anyone have an Idea why this might be?
--//SERVICES\\
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local playerData = DataStoreService:GetDataStore("PlayerData")
--//FUNCTIONS\\
local function playerJoined(player)
local cloud = Instance.new("Folder")
cloud.Name = "cloud"
cloud.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = cloud
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = cloud
local defense = Instance.new("IntValue")
defense.Name = "Defense"
defense.Parent = cloud
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Parent = cloud
local mana = Instance.new("IntValue")
mana.Name = "Mana"
mana.Parent = cloud
local superpower = Instance.new("StringValue")
superpower.Name = "SuperPower"
superpower.Parent = cloud
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
--if Data is found i.e player is not new.
for i,value in pairs(player.cloud:GetChildren()) do--Assume you have a folder with the values inside
value.Value = data[value.Name]
end
else
print("There was an error loading your data")
warn(errormessage)
end
end
local function createTable(player)
local playerStats = {}
for _, stat in pairs(player.cloud:GetChildren()) do
playerStats[stat.Name] = stat.Value
end
return playerStats
end
local function playerLeft(player) --Runs when players exit
local playerStats = createTable(player)
local success, err = pcall(function()
playerData:SetAsync(player.UserId, playerStats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
--//CONNECTS\\
game.Players.PlayerAdded:Connect(playerJoined)
game.Players.PlayerRemoving:Connect(playerLeft)