Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Health depletion script not depleting health?

I decided to make a health depletion script for a nuclear winter game. However, when I try to run it in game, nothing happens. I am a beginner and would appreciate if somebody told me what I am doing wrong.

local depletionAmount = 10

local function healthDeplete
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid then
		local currentHealth = humanoid.Health
		wait (1)
                local newHealth = currentHealth - depletionAmount
		humanoid.Health = newHealth
	end
end

3 comments

0

Is this your entire script?

0

yes. it is the entire script

0

How do you get the character variable???

1 answer

You're missing a few vital things. For one, you never defined your character. Two, you didn't put parenthesis after the function name. Three, you never called the function.

It depends how you wanted to use this, but here's an example. This would go in a LocalScript in StarterCharacterScripts.

-- player is the variable that points to the client (the player running the script) in a LocalScript.
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local depletionAmount = 10

local function healthDeplete()
	local currentHealth = humanoid.Health
	-- wait(1)
	local newHealth = currentHealth - depletionAmount
	humanoid.Health = newHealth
end

healthDeplete()

1 comment

0

Thank you for the help but the thing is I can't get it to loop no matter how many loops I try.

This discussion is preserved as it was. New answers, comments, and votes are closed.