I am trying to create a script that makes a data storage for a player when he/she joins the game and update their health every second inside another IntValue, but the script is saying that there is a 'nil' value in the script.
local plr = game.Players.LocalPlayer plr.CharacterAdded:connect(function(char) local Humanoid = char:WaitForChild('Humanoid') local plrName = plr.Name local PlayerData = game.ServerStorage.PlayerNameStorage:WaitForChild(plrName.."Data") local PlayerNameValue = PlayerData:WaitForChild(plrName.."NameValue") local HealthIntValue = PlayerData:WaitForChild(plrName.."HealthValue") local MaxHealthIntValue =PlayerData:WaitForChild(plrName.."MaxHealthValue") while true do if plr ~= nil then if char ~= nil then if Humanoid ~= nil then if Humanoid.Health > 0 then HealthIntValue.Value = Humanoid.Health wait(1) elseif Humanoid.Health <= 0 then HealthIntValue.Value = 0 wait(1) end elseif Humanoid == nil then print("'Humanoid' not found; all relating objects removed.") end elseif char == nil then print("'char' not found; all relating objects removed.") end elseif plr == nil then print("'plr' not found; all relating objects removed.") end end end)
Here is the whole code:
game.Players.PlayerAdded:connect(function(plr) local plrName = plr.Name if game.ServerStorage.PlayerNameStorage ~= nil then local PlayerData = Instance.new('Folder') PlayerData.Parent = game.ServerStorage.PlayerNameStorage PlayerData.Name = plrName.."Data" local PlayerNameValue = Instance.new('StringValue') PlayerNameValue.Parent = PlayerData PlayerNameValue.Name = plrName.."NameValue" PlayerNameValue.Value = plrName local HealthIntValue = Instance.new('IntValue') HealthIntValue.Parent = PlayerData HealthIntValue.Name = plrName.."HealthValue" local MaxHealthIntValue = Instance.new('IntValue') MaxHealthIntValue.Name = plrName.."MaxHealthValue" MaxHealthIntValue.Parent = PlayerData plr.CharacterAdded:connect(function(char) local Humanoid = char:WaitForChild('Humanoid') if Humanoid.Health > 0 then HealthIntValue.Value = Humanoid.Health MaxHealthIntValue.Value = Humanoid.MaxHealth end end) end end) game.Players.PlayerRemoving:connect(function(plr) local plrName = plr.Name if game.ServerStorage.PlayerNameStorage:FindFirstChild(plrName.."Data") ~= nil then local PlayerData = game.ServerStorage.PlayerNameStorage:FindFirstChild(plrName.."Data") PlayerData:Destroy() end end) local plr = game.Players.LocalPlayer plr.CharacterAdded:connect(function(char) local Humanoid = char:WaitForChild('Humanoid') local plrName = plr.Name local PlayerData = game.ServerStorage.PlayerNameStorage:WaitForChild(plrName.."Data") local PlayerNameValue = PlayerData:WaitForChild(plrName.."NameValue") local HealthIntValue = PlayerData:WaitForChild(plrName.."HealthValue") local MaxHealthIntValue =PlayerData:WaitForChild(plrName.."MaxHealthValue") while true do if plr ~= nil then if char ~= nil then if Humanoid ~= nil then if Humanoid.Health > 0 then HealthIntValue.Value = Humanoid.Health wait(1) elseif Humanoid.Health <= 0 then HealthIntValue.Value = 0 wait(1) end elseif Humanoid == nil then print("'Humanoid' not found; all relating objects removed.") end elseif char == nil then print("'char' not found; all relating objects removed.") end elseif plr == nil then print("'plr' not found; all relating objects removed.") end end end)