HumanoidStateType check stops working after respawn

I’m trying to make the Oxygen bar from my last post tween upward when the player starts swimming. I’m still in the Debug phase, so I’m just using print() and warn() statements currently. My problem is that when the player dies, it doesn’t connect back to the Humanoid properly, and it doesn’t print its state on change.

Image


In the above image, I jumped around, then went into water and reset. After I respawned, I moved around some more and it didn’t print the state changes in the new life.

Code & Place Download
-- LocalScript
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
warn("I found my Humanoid!")

plr.CharacterAdded:Connect(function(character)
	char = character
	hum = char:WaitForChild("Humanoid")
	warn("I found my new Humanoid!")
end)

-- vv This stuff is for when I figure out what the hell is going on lmao :') vv
local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut)

local posIn = UDim2.new(0, 20, 1, -5)
local posOut = UDim2.new(0, 20, 1.15, -5)

local tween_In = tweenService:Create(script.Parent.bgFrame, tweenInfo, {Position = posIn})
local tween_Out = tweenService:Create(script.Parent.bgFrame, tweenInfo, {Position = posOut})
-- ^^ This stuff is for when I figure out what the hell is going on lmao :') ^^

hum.StateChanged:Connect(function(a, newState)
	if newState == Enum.HumanoidStateType.Swimming then
		print("I'm swimming!")
	elseif newState == Enum.HumanoidStateType.Dead then
		print("Oops, I died.")
	else
		print("I'm doing something else. ("..tostring(newState)..")")	
	end
end)

this.rbxl (99.0 KB)
LocalScript is in game.StarterGui.OxygenGui. it’s called “tween up while swimming”

I would very much appreciate help with this. Thank you in advance!

1 Like

o btw i cant make it reset on spawn because see this post

The event is connected to an old humanoid. You have to reconnect the state change function when there’s a new humanoid because it doesn’t automatically update. Happy coding!

1 Like

I tried that already. That’s what this is for:

Did I do this wrong? Can you give the correct method?

Not exactly. You set hum to a new humanoid, but that doesn’t make the actual function connected to state change update to listen for the new humanoid’s state change. Since the variable was changed the function became invalid. Here would be the proper code for your situation:

-- LocalScript
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
local connection
warn("I found my Humanoid!")

-- vv This stuff is for when I figure out what the hell is going on lmao :') vv
local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut)

local posIn = UDim2.new(0, 20, 1, -5)
local posOut = UDim2.new(0, 20, 1.15, -5)

local tween_In = tweenService:Create(script.Parent.bgFrame, tweenInfo, {Position = posIn})
local tween_Out = tweenService:Create(script.Parent.bgFrame, tweenInfo, {Position = posOut})
-- ^^ This stuff is for when I figure out what the hell is going on lmao :') ^^

local function stateChanged(a, newState)
if newState == Enum.HumanoidStateType.Swimming then
   	print("I'm swimming!")
   elseif newState == Enum.HumanoidStateType.Dead then
   	print("Oops, I died.")
   else
   	print("I'm doing something else. ("..tostring(newState)..")")	
   end
end

plr.CharacterAdded:Connect(function(character)
   char = character
   hum = char:WaitForChild("Humanoid")
if connection then connection:Disconnect() end
connection = hum.StateChanged:Connect(stateChanged)
   warn("I found my new Humanoid!")
end)
connection = hum.StateChanged:Connect(stateChanged)
1 Like

How would I update the function to use the new humanoid then?

@melo I now have code above :slight_smile: . Hope this helps! Edit: also cleaned up the code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.