Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I disable Swimming on NPC's?

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)

1 comment

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!

2 answers

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)

2 comments

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!

0

I might mess around with body velocities. it would be similar to your answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.