Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
KarlXYZ 120
11 years ago

This is my current script:

1local core = script.Parent
2local health = script.Parent.health.Value
3 
4core.Touched:connect(function(s)
5    local p = game.Players:getPlayerFromCharacter(s.Parent.Parent)
6if (s.Name == "Handle") and (p.TeamColor == BrickColor.new("Bright red")) then
7    health = health - 20
8end
9end)

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

Log in to vote
1
Answered by
Asleum 135
11 years ago

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...

1local health = script.Parent.health

And line 7 to this...

1health.Value = health.Value - 20
0
Thank you, worked perfectly. KarlXYZ 120 — 11y
Ad
Log in to vote
0
Answered by 11 years ago

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:

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

Answer this question