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)
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.
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:
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.
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.
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.
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.