How do I assign the humanoid as a global variable in this situation?

  1. How can I declare humanoid as a global variable without breaking the script? (further)

  2. The issue is I can’t use humanoid.Health = 0 to kill the player. I assume its because the script cannot read the local humanoid variable on line 6.
    (I also cannot use Breakjoints() to kill the player as it disables the ragdolls i have on.)

  3. I’ve tried adding game.Players.CharacterAdded to declare a humanoid variable as seen on line 5, however it indents line 6’s variable too far to be read by line 21.

local decrease_frequency = 1
local decrease_amount = 1

game.Players.PlayerAdded:Connect(function(player)
	game.Players.CharacterAdded:Connect(function(char)
		local humanoid = char.FindFirstChild("Humanoid")
	--Thirst Code
	local Thirst = Instance.new("IntValue", player)
	Thirst.Name = "Thirst"
	
	Thirst.Value = 0 --max Thirst
	
	player.CharacterAdded:Connect(function()
	Thirst.Value = 0 --resets on death

	end)

	while wait(decrease_frequency) do
		--run the code here.
		if Thirst.Value >= 100 then
				humanoid.Health = 0
			end
		end
		Thirst.Value += decrease_amount
	end)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I’m not sure that I am completely understanding here. :sweat_smile:. From what I understand, you could make an ObjectValue and assign the humanoid to it and put it in ReplicatedStorage or someplace different! Again please tell me if I’m misunderstanding this scenario!

Like Im trying to make it so the humanoid variable can be used anywhere in the script, however i think its indented too much to be used on line 21

CharacterAdded is an event for the Player object, you are trying to use it with the Players service.

local decrease_frequency = 1
local decrease_amount = 1

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:FindFirstChild("Humanoid") -- changed period to a colon
		--Thirst Code
		local Thirst = Instance.new("IntValue", player)
		Thirst.Name = "Thirst"

		Thirst.Value = 0 --max Thirst
		
		-- This section is completely unnecessary because it is set to zero on spawn right above
		
		--player.CharacterAdded:Connect(function()
		--	Thirst.Value = 0 --resets on death

		--end)

		while wait(decrease_frequency) do -- would recommend task.wait(decrease_frequency) but its your choice.
			--run the code here.
			Thirst.Value += decrease_amount -- Moved this inside the while loop so it actually decreases
			
			if Thirst.Value >= 100 then
				humanoid.Health = 0
				break -- End the while loop after the player dies
			end
		end
	end)
end)

Thank you! this did the job, I was searching all over on how to nest the PlayerAdded and CharacterAdded events :sob:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.