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

01left=script.Parent:WaitForChild'LeftFoot'
02right=script.Parent:WaitForChild'RightFoot'
03humanoid=script.Parent:WaitForChild'Humanoid'
04 
05function touch()
06    if last<-64 then
07        script.Parent.Humanoid:TakeDamage((math.ceil(script.Parent.LowerTorso.Velocity.y/-4)))
08    end
09end
10 
11left.Touched:connect(touch)
12right.Touched:connect(touch)
0
Why not make a CanCollide false part above the ground do this? Goulstem 8144 — 8y

1 answer

Log in to vote
0
Answered by 8 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:

01left=script.Parent:WaitForChild'LeftFoot'
02right=script.Parent:WaitForChild'RightFoot'
03humanoid=script.Parent:WaitForChild'Humanoid'
04last = 0
05 
06function touch()
07    if last<-64 then
08        script.Parent.Humanoid:TakeDamage((math.ceil(last/-4)))
09    end
10end
11 
12left.Touched:connect(touch)
13right.Touched:connect(touch)
14 
15while wait(0.2) do
16    last = script.Parent.LowerTorso.Velocity.y
17end

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