Part that give speed then unspeed

Hi, I’m trying to do a part that give speed to the player and when he goes out the part he’s normal speed go back. I did the first step which is to give speed to the player when he’s on the part but I’m failing for the second step, can someone help me pls ?

Here is my code :

local MaPart = script.Parent

local function speed(TouchedPart)
	local character = TouchedPart.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		humanoid.WalkSpeed = 50
	end
	
end

local function Unspeed(TouchedPart)
	local character = TouchedPart.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		humanoid.WalkSpeed = 16
	end

end

MaPart.Touched:Connect(speed)
MaPart.TouchEnded:Connect(Unspeed)

maybe add a check if it actually is a player, otherwise it will detect everything that is touching.

He technically already is through the logic:

if humanoid then

He still needs to check if object actually has a parent or the script will crash. Also, :findfirstChild i recommend FindFirstChildOfClass(). And add it in the check because it will cause an error I think.

This script you’ve provided works.

Does the part you’re referencing have CanTouch enabled?

Yep, It’s enable, however It doesn’t work.

It’s giving some random little boost, I don’t want that.

I’ve just tried it in Studio. Could you provide a gif or a game file?

I tested the script, and it seems that the script will remove the speed boost although the player is still on the part (e.g. left leg goes off to remove then right leg gives speed).

Here is a screenshot:
image
I’m not sure of the best way to fix this, but you could have a box around the part with the speed boost script so TouchEnded doesn’t fire when you don’t want.

2 Likes

Region3 could be utilised too, depends on how complex the OP wants to make this I guess.

1 Like

Like MNIXbean said; if you make the part like this, so it covers the full character, it’ll stop what he mentioned. Perhaps that’s your problem?

It’s not my issue, My purpose is to do a sort of carpet that change the speed of the player and when he’s not touching the carpet he’s speed go back at the origin(16). A bit like the thing in the airports yk.

1 Like

Right, but the carpet part doesn’t specifically have to be what gives you speed. You could have another, invisible, part which gives player a speed boost within the bounds of that bigger part.

1 Like

OOOOH, I see ty, I will try later. It’s should be work

1 Like

No problem, hope you manage to get it working how you intended!

Well I tried a thing which is a sort of what you said differently, and It doesn’t work moreover If I you like you said the problem is same because you need to alter the walk speed value of the player when you go out the block which give speed.

This script should give the player speed only when its on the part:

local part = script.Parent

local characters = {}
part.Touched:Connect(function(part)
	local character = part.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		humanoid.WalkSpeed = 50
		
		if characters[character] == nil then
			characters[character] = {}
		end
		
		characters[character][part] = true
	end
end)

part.TouchEnded:Connect(function(part)
	local character = part.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		if characters[character] == nil then
			return
		end
		
		characters[character][part] = false
		
		for part, isTouching in pairs(characters[character]) do
			if part.Parent and isTouching then
				return
			end
		end
		
		humanoid.WalkSpeed = 16
	end
end)

While this approach works for some animation packs, I fear that it might have some issues with some of the weirder packs.

2 Likes

maybe place another (invisible) part surrounding the speed boost one that reduces speed back to normal.

srry if i misunderstood ur question

I did it and It’s not what I want but ty

Ty I will try it later however It’s a bit sophisticated.

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