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

Why does my script see players as always swimming?

Asked by 7 years ago
Edited 7 years ago

This is the log over 8.5 seconds: 21:32:32.155 - Baseplate was auto-saved (x2) swimming (x17)

local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local sprint = false

local t,f = true,false

local w,a,s,d = f,f,f,f
local wt = 0

function lower() -- player starts drowning if they are swimming
    c.Humanoid.Health = c.Humanoid.Health -1
end


m.KeyDown:connect(function(key)
    if key == '0' then
        sprint = t
    end
    if key == 'w' then
        w = t
    end
    if key == 'a' then
        a = t
    end
    if key == 's' then
        s = t
    end
    if key == 'd' then
        d = t
    end
end)
m.KeyUp:connect(function(key)
    if key == '0' then
        sprint = f
    end
    if key == 'w' then
        w = f
    end
    if key == 'a' then
        a = f
    end
    if key == 's' then
        s = f
    end
    if key == 'd' then
        d = f
    end
end)

while wait() do
    wt = wt - .01
    if wt <= 0 then
        if script.Parent.Frame.Bar.Size.X.Scale < .9 then
            script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(.009,0,0,0)
        end
    end
    if w or a or s or d then
        if script.Parent.Frame.Bar.Size.X.Scale > 0 and sprint then
            script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(.009,0,0,0)
            c.Humanoid.WalkSpeed = 20
        end
    end
    if sprint then
        wt = 1.3
    end
    if not w and not a and not s and not d then c.Humanoid.WalkSpeed = 12 end
    if not sprint then c.Humanoid.WalkSpeed = 12 end
    if script.Parent.Frame.Bar.Size.X.Scale <= 0 then c.Humanoid.WalkSpeed = 12 end
    if c.Humanoid.Swimming:connect(lower) then print("swimming") wait(0.5)end -- detects if a player is swimming
    end


1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Swimming is an event of the Humanoid object.. you can't "check" an RbxScriptSignal and expect it to return if the player is swimming or not.

I suggest you use the StateChanged event of the Humanoid object. It fires when the state of the humanoid changes(e.g. Jumping, Falling, Dead, swimming). It returns the previous state of the humanoid, and the current state.

You can compare the second returned value with the HumanoidStateType Enum.

local p = game.Players.LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local h = c:WaitForChild("Humanoid")
local target_state = Enum.HumanoidStateType.Swimming
local swimming = false

h.StateChanged:Connect(function(old,new)
    if new == target_state then --Compare current state
        swimming = true
        print(c.Name.." went from "..tostring(old).." to "..tostring(new).."!")
    else
        swimming = false
    end
end)
Ad

Answer this question