Review this simple trampoline script

Hi, i created this simple trmpoline for jumping purposes! The only flaw it’s that sometimes it can be laggy.
Can you review my code?

local JP = 10
script.Parent.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
human.JumpPower = JP
human.Jump = true
wait(0.1)
human.JumpPower = 50
end
end)
2 Likes

What do you mean by laggy? Is it not always bouncing up to its height that it’s supposed to? I’m guessing that because I used to have a problem like that.

1 Like

Sometimes it takes some time to the player to actually bounce, and there’s a rare chance he won’t actually jump

Can you send me a GIF? I don’t really understand that they “rarely jump”.

gif
There (The character auto-jumps)

Maybe add a debounce or some sort of cooldown?

3 Likes

He did, he put wait(0.1) Thats a cooldown, but a very short one.

1 Like

Try adding a debounce:

local JP = 10
local db = false
script.Parent.Touched:Connect(function(hit)
   local human = hit.Parent:FindFirstChild("Humanoid")
   if human and db == false then
     db = true
     human.JumpPower = JP
     human.Jump = true
     wait(0.1)
     human.JumpPower = 50
     wait(1)
     db = false
  end
end)

A debounce is needed because the touched event fires more than once. Using one means we ignore all other touched events besides the first one.

Also, if you’re going to post code on here, please properly indent it.

2 Likes

It wasn’t a proper debounce. As far as I’m concerned it will keep on running as if it was another thread.(correct my terminology if my wrong)

Part of the “lag” problem has to do with the fact that the Touched event is bad at responding to Humanoids. When you land on a part, the Touched event will not fire until the character finishes “landing” and settles after a moment. The FloorMaterial property of the Humanoid does update instantly, though, so you can work around the broken Touched behavior with an ugly hack by listening for the FloorMaterial to change from Air. The only other problem here is that a JumpPower of 10 isn’t high enough for the FloorMaterial to change, so really it’s going to require a precise calculation to determine when the character should land given how high they last jumped, and a raycast to check if the character is landing on the trampoline part.

This would be much easier if the Touched event just responded properly to jumping humanoids.

an additional workaround would be to place an invisible, non-colliding “hitbox” part above the trampoline, so then the legs will make contact with it immediately and fire the Touched event instead of the humanoid stopping and hovering over it for a bit.

3 Likes

I found a much simple solution! i just made the wait period longer.

I would guess you’re still seeing it jump like this
jump0
instead of like this
jump1
because of the time it takes the jump animation to let the foot touch the part after the humanoid lands. I assumed that was the “lag” you were talking about.

2 Likes

I’d like to add you’d probably be better off putting this in a local script otherwise the debounce will take place for everyone. Or you could make a table of players and have a debounce value inside it

wait, that means humanoid jumppower replicates?

Yeah, it represents the problem, but if i make the script have a small delay between jumps, it’s like nothing is happening

Player has network ownership of its own character. It’ll replicate

Strange, woudn’t that be a giant gateway to exploits?

Somewhat yes, but you can easily prevent some of these exploits like walkspeed by comparing their last positions each heartbeat in the server

1 Like

It’s not the property that replicates, it’s the physics. The same goes for WalkSpeed. The actual property doesn’t replicate, the changes in physics this creates does.

cc @Jrelvas1

The check Frost provided will work in terms of fighting against movement exploits (probably).

1 Like