TouchEnded and Touched firing when jumping inside zone

I’m creating an invisible zone that affects the player’s walkspeed. I want it to only fire when the player enters or leaves the zone, ignoring any actions within the zone. I have a script that increases the localplayers walkspeed by 15 when they jump. However, if they jump inside the zone, I want it to add 15 to their current walkspeed instead of overriding it to the zone’s default of 20.

When the player jumps within the zone, it overrides their walkspeed and sets them back to the set zone walkspeed.

I tried adding an if statement that checks if the player has the “WalkspeedRegion” child and returns if so, but the TouchEnded and Touched events still fire regardless.

local original_speed = 16 
local new_speed = 20

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("WalkspeedRegion") then
		local x = Instance.new("BoolValue", hit.Parent);
		x.Name = "WalkspeedRegion"
		hit.Parent.Humanoid.WalkSpeed = new_speed
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("WalkspeedRegion") and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.WalkspeedRegion:Destroy()
		hit.Parent.Humanoid.WalkSpeed = original_speed
	end
end)

Can I see the size of your zone? Also, one thing you can do is to check if the player’s walkspeed is more than the zone speed, which would mean that they are jumping, and make it avoid changing your walkspeed then.

im pretty sure touchended fires and touched fires again constantly for the same part if youre moving through another part im not sure why that happens even though youre still touching it. maybe consider using GetPartsInBoundingBox or something like that

This is the current size of the zone.

Alright so that gets rid of one of my concerns which is that the player is jumping out of bounds. Try using the condition I told you about in my previous post.

GetPartsInBoundingBox() is a good idea but you would have to use a loop and idk if that is more efficient. Idk, up to OP tho.

1 Like

I tried out the condition you provided, and the result is that the zone continues to override the localplayer walkspeed even though it’s supposed to return if hit.Parent.Humanoid.WalkSpeed > new_speed.

Is the code where you change the player’s walkspeed when jumping on the client?

Yeah, it is. It is under StarterCharacterScripts.

Use the ZonePlus module

Way more reliable than Touched and TouchedEnded

This should help.

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("WalkspeedRegion") and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
		
		hit.Parent.WalkspeedRegion:Destroy()
		hit.Parent.Humanoid.WalkSpeed = original_speed
	end
end)

It checks if the player is in a jumping state before executing the touch ended code.

1 Like

I mean is it a local or server script?
Also @NefariouslyScripted that ignores falling and I think it’s Touch connection that is breaking.

The walkspeed script is a local script.

You have to change it to server because client changes don’t replicate onto the server.

And because you are swapping to the server you have to change your code a bit.

Use Region3 not Touched. The best you can do with touched is add a debounce

1 Like

zoneplus is also a good alternative to touched from what I know

Region3 is best an easiest method in my opinion.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.