I’m currently working on a game where you jump on enemies to damage them, and when you jump on them, you also get an extra jump, like in Super Mario. However, I’m using the touched event on a part welded to the enemy’s root part (the enemy is a humanoid) to detect when a player touches it, and 25% of the time, the player does not get a slight jump after jumping on the enemy, and I’ve figured out that it’s the touched event not firing on the part welded to the enemy’s root.
Here’s the code that’s in a local script, but I’m certain it’s safe:
hitbox.Touched:Connect(function(hit) --The hitbox here is a part welded to the player's root to detect jumping on enemies
print("debounce:", debounce, "attr:", hit.Parent:GetAttribute("Damage"), "health:", hit.Parent.Humanoid.Health, "name:", hit.Name, "ancest:", hit:FindFirstAncestor("Enemies"))
--Here we check if the player hit a part welded to the enemy's root
if not debounce and hit:FindFirstAncestor("Enemies") and not hit.Parent:GetAttribute("Damaged") and hit.Name == "JumpHitbox" and hit.Parent.Humanoid.Health > 0 then
debounce = true
print("set to true")
local pos = root.Position.Y
--Don't worry about this stuff
for i = 1, 3 do
runService.RenderStepped:Wait()
end
print("waited for a few frames")
print(pos - root.Position.Y)
if math.sign(pos - root.Position.Y) == 1 then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
canDoubleJump = true
print("just jumped")
else
print("did not jump")
end
print("gonna set to false")
wait(.3)
print("set to false")
debounce = false
end
end)
I’m 100% positive that this code is fine. I’ve tested it, and whenever the player doesn’t jump after jumping on the enemy, nothing in the event runs.
Here is a picture of the enemy dummy. I’ve selected both the enemy’s root and the part welded to the root. The part welded to the root has CanTouch on, but the root doesn’t. This property is not changed by any other scripts.
-
What do you want to achieve? I want to make the player jump after jumping on an enemy.
-
What is the issue? 25% of the time, the touched event doesn’t fire when the player touches the part welded to the enemy’s root.
-
What solutions have you tried so far? I tried detecting the enemy’s root instead of the part welded to the root, and had the same issue. I also tried looking up a similar issue. Nothing.
The thing is, I also check the player jumping on the enemy on the server (so I can damage it on the server) and it’s perfectly fine there, but why not here? It has pretty much the exact same code as the LocalScript I showed, so I’m completely stumped.
Also, the enemy’s root and the part welded to the root only change position when the server temporarily adds a body position in it, but quickly destroys the body position afterwards. I doubt this has any relevancy to the issue at hand, but just clarifying that the root’s position and the part welded to the root’s position never really change.