If you go in the mud your character's walk speed will change to 10 and when you get out of the mud it will go back to 16. How do you check if the player has left the mud?
1 | script.Parent.Touched:Connect( function (hit) |
2 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
3 | if humanoid ~ = nil then |
4 | humanoid.WalkSpeed = 10 |
5 | end |
6 | end ) |
https://developer.roblox.com/en-us/api-reference/event/BasePart/TouchEnded
Try using the TouchEnded RBXScriptSignal
connected to a function. It will fire whenever the touch has ended.
01 | script.Parent.Touched:Connect( function (hit) |
02 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
03 |
04 | if humanoid then |
05 | humanoid.WalkSpeed = 10 |
06 | end |
07 | end ) |
08 |
09 | script.Parent.TouchEnded:Connect( function (hit) |
10 | local humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
11 |
12 | if humanoid then |
13 | humanoid.WalkSpeed = 16 |
14 | end |
15 | end ) |
Use a touched ended event
01 | script.Parent.Touched:Connect( function (hit) |
02 | local Humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
03 | if (Humanoid ~ = nil ) then |
04 | Humanoid.WalkSpeed = 10 |
05 |
06 | script.Parent.TouchedEnded:Connect( function (hit) |
07 | local Humanoid = hit.Parent:FindFirstChild( "Humanoid" ) |
08 | if (Humanoid ~ = nil ) then |
09 | Humanoid.WalkSpeed = 16 |
10 | end |
11 | end ) |
sorry for lack of elaboration
If you use touch ended, it might be funky and your speed may flicker (and the player could just jump). Another way to do this is to check if a player is inside of a certain area. I randomly found this guide about this on script helpers here. Personally I haven't read too much of it, but it might help.
Edit: for an unrotated part, it's easy to tell if a player is in it. Assume p1 and p2 are two oppisite corners of the affected area.
01 | lp = Vector 3. new(math.min(p 1. x, p 2. x), math.min(p 1. y, p 2. y), math.min(p 1. z, p 2. z)) |
02 | up = Vector 3. new(math.max(p 1. x, p 2. x), math.max(p 1. y, p 2. y), math.max(p 1. z, p 2. z)) |
03 |
04 | local function checkifinarea(point) |
05 | if (point.x> = lp.x and point.x< = mp.x) and (point.y> = lp.y and point.y< = mp.y) and (point.z> = lp.z and point.z< = mp.z) then |
06 | return true |
07 | else |
08 | return false |
09 | end |
10 | end |