Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make this more efficient?

Asked by
ItsMeKlc 235 Moderation Voter
9 years ago

So basically I want the player's walk speed to change whenever it is in the Terrain water. What I made bellow works, but I'd like it to be more efficient, and only change when the player is in/out of the water. I don't really know how to go about doing so, and I would really appreciate some help <3

01game.Players.PlayerAdded:connect(function(Player)
02Player.CharacterAdded:connect(function(char)
03local humanoid = char:WaitForChild("Humanoid")
04local drown = false
05 
06humanoid.Swimming:connect(function(swim)
07if swim > 0 then drown = true
08elseif swim < 1 then drown = false
09end
10end)
11 
12while wait(0.1) do
13if drown then humanoid.WalkSpeed = 5
14else
15local found = game.Workspace:FindFirstChild(humanoid.Parent.Name.."'s_folder")
View all 25 lines...

Happy murica day!

1 answer

Log in to vote
1
Answered by 9 years ago

You can simply use the StateChanged event of the Humanoid to tell when the Human is swimming or when the human is not swimming. You can use it like so:

1Humanoid.StateChanged:connect(function(_, State) --The StateChanged event has 2 parameters, the previous state and the current state. We only need to know the current state so we can use an "_" to take the place of an unused parameter
2    if State == Enum.HumanoidStateType.Swimming then
3        --Swimming code
4    else
5        --Not swimming code
6    end
7end)

Hope this helped!

Ad

Answer this question