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

Decrease numbervalue when a player's takes damage?

My script doesnt work and I used getplayerfromcharacter and changedhealth? Does anyone know how to fix this? Thanks

local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)
local currentHealth = character.Humanoid
if player then
    print("Player is " .. player.Name)
humanoid.HealthChanged:Connect(function(health)
	local change = math.abs(currentHealth - health)
	print("The humanoid's health "..(currentHealth > health and "decreased by " or "increased by ")..change..".")
	currentHealth = health
workspace.MoneyValue.Value = workspace.MoneyValue.Value - 1401
else
    print("Player doesn't exist!")
end

1 comment

0

what line

3 answers

You mentioned character before player.

local player = game.Players:GetPlayerFromCharacter(character)
local character = game.Workspace.player
local currentHealth = character.Humanoid
if player then
    print("Player is " .. player.Name)
humanoid.HealthChanged:Connect(function(health)
    local change = math.abs(currentHealth - health)
    print("The humanoid's health "..(currentHealth > health and "decreased by " or "increased by ")..change..".")
    currentHealth = health
workspace.MoneyValue.Value = workspace.MoneyValue.Value - 1401
else
    print("Player doesn't exist!")
end

4 comments

0

where do i put this

0

.-. wdym

0

what folder, what type of script

0

Look up GetPlayerFromCharacter iiDkOfficial. That is how you use it.

In Line 6 You forgot the period between Health and Changed. And since you are accessing the Humanoid property you need to have a path to Humanoid since you didn't assign it a path.

humanoid.HealthChanged:Connect(function(health)

So it should be

character.Humanoid.Health.Changed:Connect(function(health)
local character = game.Workspace.Player
local player = game.Players:FindFirstChild(character.Name)
local currentHealth = character.Humanoid
if player then
    print("Player is " .. player.Name)
humanoid.Health.Changed:Connect(function(health)
    local change = math.abs(currentHealth - health)
    print("The humanoid's health "..(currentHealth > health and "decreased by " or "increased by ")..change..".")
    currentHealth = health
workspace.MoneyValue.Value = workspace.MoneyValue.Value - 1401
else
    print("Player doesn't exist!")
end

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