How can I declare humanoid as a global variable without breaking the script? (further)
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.)
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. . 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!
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)