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

Variable won't update within damage script?

Asked by 4 years ago

I have a simple damage script that deals damage to the player on touch, per usual. I have a variable for defense that is pulled from a NumberValue object that the player's character model gets when the player joins the server or respawns. However the defense variable within the script never updates. Any help?

function onTouched(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")

    if (human ~= nil) then

        local Defense = hit.Parent.CharDefense

        local Damage = 1 - (Defense.Value / 100)
        print(Damage)
        human.Health = human.Health - Damage

    end
end

script.Parent.Touched:connect(onTouched)
0
it works fine for me, check if you didnt misspell something or if you get any errors when you test it Lazarix9 245 — 4y
0
I don't get any errors and the script does work. To be more specific, when the NumberValue for defense is updated in the player model, the value is not updated within the script. So every time damage is taken it is using the defense value the player had when the player joined the game, and never updating. kasparov2 12 — 4y

2 answers

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago

How high is your Defence Value?

Try having the local value outside of the functions scope. That might be your problem.

0
How would I go about doing this, since the parameter of the function is referenced when indexing the defense value? Thank you for your help! kasparov2 12 — 4y
0
HAve you tried what I said? Farsalis 369 — 4y
0
How would I go about doing what you are suggesting, is what I mean. I tried what I think you mean (moving the local Defense above the function) and it just breaks the script. kasparov2 12 — 4y
0
Ok, when you declare defence, put in the value instead of acessing it later. e.g.(Defence = CharDefence.Value) Farsalis 369 — 4y
View all comments (2 more)
0
Thank you for your help I figured it out kasparov2 12 — 4y
0
what did you do that worked? Farsalis 369 — 4y
Ad
Log in to vote
0
Answered by
NSMascot 113
4 years ago

try this, im not sure if it will work

script.Parent.Touched:Connect(function(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")

    if (human ~= nil) then

        local Defense = hit.Parent.CharDefense

        local Damage = 1 - (Defense.Value / 100)
        print(Damage)
        human.Health = human.Health - Damage

    end
end)

Answer this question