I’ve been making this script for several days now and I’m so confused on why this is happening. Whenever I hold the tool out inside of water my character gets frozen in place to the point where I can only change the direction I’m facing. I would be so grateful to anyone who could help me.
local tool = script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function printHumanoidState()
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
if not humanoid or not character then return end
print("Current humanoid state: ", humanoid:GetState())
end
local function onEquipped()
print("Tool has been equipped.")
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
if not humanoid or not character then return end
local sitanimation = Instance.new("Animation")
sitanimation.AnimationId = "rbxassetid://16010590992"
if humanoid:GetState() == Enum.HumanoidStateType.Swimming then
print("Player is swimming.")
local bodyPosition = Instance.new("BodyPosition")
bodyPosition.Position = character.HumanoidRootPart.Position + Vector3.new(0, 5, 0)
bodyPosition.Parent = character.HumanoidRootPart
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, 0, 0)
bodyVelocity.Parent = character.HumanoidRootPart
sitanimationTrack = humanoid:LoadAnimation(sitanimation)
sitanimationTrack:Play()
end
local function isTouchingWater()
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
if not humanoid or not character then return false end
if humanoid.FloorMaterial == Enum.Material.Water then
return true
else
return false
end
end
if isTouchingWater() then
humanoid:SetStateEnabled("Swimming", true)
end
end
local function onUnequipped()
print("Tool has been unequipped.")
local character = player.Character
local humanoid = character and character:FindFirstChild("Humanoid")
if sitanimationTrack then
sitanimationTrack:Stop()
sitanimationTrack.Stopped:Wait()
end
if character then
if character.HumanoidRootPart:FindFirstChild("BodyPosition") then
character.HumanoidRootPart.BodyPosition:Destroy()
end
if character.HumanoidRootPart:FindFirstChild("BodyVelocity") then
character.HumanoidRootPart.BodyVelocity:Destroy()
end
end
end
local function swimno()
script.Parent.Parent:WaitForChild("Humanoid"):SetStateEnabled("Swimming", false)
end
local function swimyes()
script.Parent.Parent.Parent.Character:WaitForChild("Humanoid"):SetStateEnabled("Swimming", true)
end
tool.Equipped:Connect(onEquipped)
tool.Equipped:Connect(swimno)
tool.Unequipped:Connect(onUnequipped)
tool.Unequipped:Connect(swimyes)
while wait(1) do
printHumanoidState()
end