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

How can I disable Swimming on NPC's?

Asked by 3 years ago

So here is my script. ServerScript located into the NPC. The NPC takes a nose dive into water as he should based on standard roblox physics, I'm looking for him to float across the top, or at least not nose dive. I would create an animation if needed, I just was hoping to simply insert this script into future NPCs to avoid animating tons of future various NPCs.

I feel like this should be simple, but i've messed around with but didn't manage to get it to work.

Thank you!

function swim()
    if script.Parent.Humanoid:GetState()==Enum.HumanoidStateType.Swimming then
        script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
    end
end
script.Parent.Humanoid.StateChanged:Connect(swim)
0
I have used a body velocity to make it float on the water. but if someone could please help me execute this script it will stop the nose dive. Thanks! DemonsEmperor 26 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Delete the animation, but just to be sure, If there is any water near an NPC put a part over the water and add a LocalScript And Put This In

game.players.PlayerAdded:Connect(function()
local NPCFoot = script.parent.LeftFoot --Put this in the npc, Not in any parts
local WaterBlocker = game.Workspace.WaterBlocker
if WaterBlocker.Touched:Connect(function(NPCFoot)
  then
WaterBlocker.CanCollide = true
end)
0
that would make my players not able to swim. Basically i have a crocodile and I dont want fighting him in the water an easy out. I love the creativity of the answer though! DemonsEmperor 26 — 3y
0
I might mess around with body velocities. it would be similar to your answer DemonsEmperor 26 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Weirdly, this type of code will only work in a local script. Also, SetStateEnabled is a toggler, you do not have to use it based on an event, just use it once (it's basically like flipping a switch).

I have tested this inside a local script and it will work just fine. Obviously parent it to a service that will place it to the player like StarterPlayerScripts.

local c = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = c:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)

Even though it is in a local script, the character is controlled by the client and replicates to the server so it would virtually be the same as a server script doing it anyways.

Answer this question