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

How can I make this IntValue decrease when it's parent is hit by a sword?

This is my current script:

local core = script.Parent
local health = script.Parent.health.Value

core.Touched:connect(function(s)
	local p = game.Players:getPlayerFromCharacter(s.Parent.Parent)
if (s.Name == "Handle") and (p.TeamColor == BrickColor.new("Bright red")) then
	health = health - 20
end
end)

health is the IntValue that's a child of core, it's set at 5000.

Handle is the name of the brick of the sword that I'm using.

I would like it to only decrease health when the player with the sword is on the Bright red team.

Whenever I am testing it, the health value simply stays at 5000 and doesn't decrease, I don't get any errors related to the script in output...

2 answers

✓ Accepted answer

Here, you're editing a variable that's stored inside your script, not the value property of the IntValue object, that's why, when you decrease health, it doesn't impact the value of your IntValue. Here's how to do it : change line 2 to this...

local health = script.Parent.health

And line 7 to this...

health.Value = health.Value - 20

1 comment

0

Thank you, worked perfectly.

You need to find the Humanoid touched by the sword, then make a delay or everytime the sword is collided with the humanoid and it moves it will takes damage:

local core = script.Parent
local health = core:FindFirstChild("health") -- Check for the IntValue, don't get without checking, never!
 
core.Touched:connect(function(s)
	local h = s.Parent:FindFirstChild("Humanoid")
	--Don't define an instance for a color
	if (p.TeamColor == "Bright red") then -- It calls the function automatically in your "core" variable
		health.Value = health.Value - 20
	end
end)

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