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

Is there a way to get the last value of a object?

Asked by 7 years ago

I am trying to make a script that damages the player if he falls too hard,but when the Touch event is fired,the velocity of the player has already changed. It would really help me if there were like,i don't know, BindToTouch or BindToChange event in objects,unfortunally,there isn't.

Here is the script that i made


left=script.Parent:WaitForChild'LeftFoot' right=script.Parent:WaitForChild'RightFoot' humanoid=script.Parent:WaitForChild'Humanoid' function touch() if last<-64 then script.Parent.Humanoid:TakeDamage((math.ceil(script.Parent.LowerTorso.Velocity.y/-4))) end end left.Touched:connect(touch) right.Touched:connect(touch)
0
Why not make a CanCollide false part above the ground do this? Goulstem 8144 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Since the .Changed event of a BasePart rarely gets fired when the position changes, instead you could use a loop to keep the last value updated. Here's an example:

left=script.Parent:WaitForChild'LeftFoot'
right=script.Parent:WaitForChild'RightFoot'
humanoid=script.Parent:WaitForChild'Humanoid'
last = 0

function touch()
    if last<-64 then
        script.Parent.Humanoid:TakeDamage((math.ceil(last/-4)))
    end
end

left.Touched:connect(touch)
right.Touched:connect(touch)

while wait(0.2) do
    last = script.Parent.LowerTorso.Velocity.y
end

Hopefully this works for you as it does for me. If it does, accept it as the answer and vote up if you feel like it! If it doesn't work, comment down below what happens and I'll be more than happy to help.

Ad

Answer this question