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)

01local p = game.Players.LocalPlayer
02local c = p.Character
03local m = p:GetMouse()
04 
05local sprint = false
06 
07local t,f = true,false
08 
09local w,a,s,d = f,f,f,f
10local wt = 0
11 
12function lower() -- player starts drowning if they are swimming
13    c.Humanoid.Health = c.Humanoid.Health -1
14end
15 
View all 72 lines...

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.

01local p = game.Players.LocalPlayer
02local c = p.Character or p.CharacterAdded:Wait()
03local h = c:WaitForChild("Humanoid")
04local target_state = Enum.HumanoidStateType.Swimming
05local swimming = false
06 
07h.StateChanged:Connect(function(old,new)
08    if new == target_state then --Compare current state
09        swimming = true
10        print(c.Name.." went from "..tostring(old).." to "..tostring(new).."!")
11    else
12        swimming = false
13    end
14end)
Ad

Answer this question