[SOLVED] attempt to index nil with 'Value'

Hello,
I am working on a script that datastores a humanoids death via bool value. When you kill it, the value becomes true, and when you rejoin it goes to the health it was before. But, for some reason, I ran into a error when loading it. They are in two different scripts. This script is inside the dummy, and the error on line 3 is

attempt to index nil with 'Value'

Here is the script:

game.Players.PlayerAdded:Connect(function(Player)
	local value = script.Parent:FindFirstChild("IsDead")
	if value.Value == true then
		script.Parent.Humanoid.Health = 0
		print("Dead")
	end
	

	end)

script.Parent.Humanoid.Died:Connect(function()
	local value = script.Parent:FindFirstChild("IsDead")
	value.Value = true
end)

Here is the data storing script:

local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("Datastar")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local bool = Instance.new("BoolValue")
	bool.Name = "IsDead"
	bool.Parent = workspace.dum2

	local data
	local success, result = pcall(function()
		data = dataStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		if data then
			print(result)
			bool.Value = data
		else
			print(result)
			bool.Value = false
		end
	else
		warn(result)
	end
end)

players.PlayerRemoving:Connect(function(player)	
	local success, result = pcall(function()
		dataStore:SetAsync("Player_"..player.UserId, player.Bool.Value)
	end)

	if success then
		print(result)
	else
		warn(result)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(players:GetPlayers()) do
		local success, result = pcall(function()
			dataStore:SetAsync("Player_"..player.UserId, player.Bool.Value)
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end)

1 Like

Try using :WaitForChild() instead, if that doesn’t work then you are referencing or creating the value in the wrong place

Theres no more errors, but on rejoining the dummy’s health is still the same.

If you’re only looking to have the dummy be either dead or alive, or if you’re trying to save the health exactly, I don’t see any code that applies either, or I’m just dumb

	if value.Value == true then
		script.Parent.Humanoid.Health = 0
		print("Dead")
	end
script.Parent.Humanoid.Died:Connect(function()
	local value = script.Parent:FindFirstChild("IsDead")
	value.Value = true
end)

These two blocks determine the health based on the bool value.

Maybe you’re checking the values before it actually gets updated to the datastore

This will throttle if you’re damaging the dummy too much. Regardless, here’s an example of how I made something like this:

local Dummy = script.Dummy
local DummyHumanoid = Dummy:WaitForChild("Humanoid")

local DataStore = game:GetService("DataStoreService")
local HealthData = DataStore:GetDataStore("NpcHealth")

function saveNpcHealth()
	local HumanoidHealth = {
		Health = DummyHumanoid.Health,
		MaxHealth = DummyHumanoid.MaxHealth,
	}
	HealthData:SetAsync(Dummy.Name .. "-healthdata", HumanoidHealth)
end

function loadNpcHealth()
	local HumanoidsHealth = HealthData:GetAsync(Dummy.Name .. "-healthdata")
	if HumanoidsHealth then
		DummyHumanoid.MaxHealth = HumanoidsHealth.MaxHealth
		DummyHumanoid.Health = HumanoidsHealth.Health
	end
end

Dummy.Humanoid.HealthChanged:Connect(function()
	saveNpcHealth()
end)
loadNpcHealth()

Dummy.Parent = workspace

My issue fixed after just adding a wait, thanks a lot for your time though. I end up using this if its a better solution.