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

game.Players.PlayerAdded:connect(function(Player)
Player.CharacterAdded:connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
local drown = false

humanoid.Swimming:connect(function(swim)
if swim > 0 then drown = true
elseif swim < 1 then drown = false
end
end)

while wait(0.1) do
if drown then humanoid.WalkSpeed = 5
else
local found = game.Workspace:FindFirstChild(humanoid.Parent.Name.."'s_folder")
if found then
    humanoid.WalkSpeed = found.Speed.Value
end

end
end

end)
end)
****

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:

Humanoid.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
    if State == Enum.HumanoidStateType.Swimming then
        --Swimming code
    else
        --Not swimming code
    end
end)

Hope this helped!

Ad

Answer this question