Else statement not running in code

Hey everyone!
I’m creating a game where the user can detach their head and use it for various tasks (lol) and for some reason when the player presses the button for the head to return back to them, the code doesn’t run. I tried rearranging code and whatnot but still the same issue. Here’s a recording of the bug. If plr.Character.ThrowingHead.Value is already true it should return the head back to the player.

My code:

local ts = game:GetService("TweenService")
local info = TweenInfo.new(.8, Enum.EasingStyle.Circular, Enum.EasingDirection.In, 0, false)
local RaycastHitbox = require(game.ReplicatedStorage.RaycastHitboxV4)

game.ReplicatedStorage.ThrowHead.OnServerEvent:Connect(function(plr)
	print(plr.Character.ThrowingHead.Value)
	if plr.Character.Humanoid.FloorMaterial == Enum.Material.Air then return end
	local startTime = tick()
	local char = plr.Character
	if char.ThrowingHead.Value == true then
		print("exiting")
		for i,v in pairs(workspace:GetChildren()) do
			if v:FindFirstChild("Owner") and v.Owner.Value == plr.Name then
				local throwHead = v
				local char = plr.Character
				local currentHead = char.Head

				local headPos = throwHead.CFrame
				local goal2 = currentHead.CFrame
				local connection


				connection = game:GetService("RunService").Heartbeat:Connect(function()
					local currentTime = tick() - startTime
					headPos = throwHead.CFrame
					goal2 = currentHead.CFrame
					local alpha =ts:GetValue(currentTime/2, Enum.EasingStyle.Circular, Enum.EasingDirection.In)
					if alpha <1 then
						throwHead.CFrame = headPos:Lerp(goal2, alpha)

					else

						currentHead.Transparency = 0
						plr.Character.HumanoidRootPart.Anchored = false
						game.ReplicatedStorage.EndHead:FireClient(plr)
						plr.Character.ThrowingHead.Value = false
						throwHead:Destroy()
						connection:Disconnect()


					end

				end)

			end 

		end

	else 

		print('starting')
		if char.Stamina.Value <=0 then return end
		char.ThrowingHead.Value = true
		local currentHead = char.Head
		local hrp = char.HumanoidRootPart
		hrp.Anchored = true
		local throwHead = currentHead:Clone()
		for i,v in pairs(throwHead:GetChildren()) do
			v:Destroy()
		end
		throwHead.Name = "ThrowHead"
		throwHead.Anchored = true
		local owner = Instance.new("StringValue", throwHead)
		local a = Instance.new("Attachment", throwHead)
		a.Name = "DmgPoint"
		owner.Name = "Owner"
		owner.Value = plr.Name
		throwHead.Parent = workspace
		currentHead.Transparency = 1
		game.ReplicatedStorage.ThrowHead:FireClient(plr, throwHead)

		local newHitbox = RaycastHitbox.new(throwHead)
		newHitbox.DetectionMode = 2
		newHitbox.OnHit:Connect(function(hit)
			if hit:IsA("BasePart") and hit:FindFirstChild("Touch") then
				print(hit.Name)
				hit.Touch.Value = true

			end

		end)	

		local connection4
		local c3 
		local draining = true
		c3 = game:GetService("RunService").Heartbeat:Connect(function()
			if char.ThrowingHead.Value == true and throwHead ~= nil and newHitbox ~= nil and draining == true then

				newHitbox:HitStart()
				wait(2)

			else
				c3:Disconnect()
			end
		end)



		connection4 = game:GetService("RunService").Heartbeat:Connect(function()
			if char.ThrowingHead.Value == true then
				local camLv = char.CamPos.Value
				local camRotation = math.atan2(-camLv.X, -camLv.Z)
				throwHead.CFrame = CFrame.new(throwHead.Position) * CFrame.Angles(0, camRotation, 0)


				if char.MovingForward.Value == true then
					wait(.02)
					throwHead.CFrame = throwHead.CFrame * CFrame.new(0,0,-1)

				end

				if char.MovingUp.Value == true then
					wait(.02)
					throwHead.CFrame = throwHead.CFrame * CFrame.new(0,0.5,0)

				end
				if char.MovingDown.Value == true then
					wait(.02)
					throwHead.CFrame = throwHead.CFrame * CFrame.new(0,-0.5,0)

				end
			else
				connection4:Disconnect()
			end
		end)

		spawn(function()
			while draining == true do
				wait()
				if char.ThrowingHead.Value == true and char.Stamina.Value >0 then

					char.Stamina.Value -= 1

					wait(.5)
				else
					if char.Stamina.Value == 0 then

						for i,v in pairs(workspace:GetChildren()) do
							if v:FindFirstChild("Owner") and v.Owner.Value == plr.Name then
								local throwHead = v
								local char = plr.Character
								local currentHead = char.Head

								local headPos = throwHead.CFrame
								local goal2 = currentHead.CFrame
								local connection
								local hitbox = RaycastHitbox:GetHitbox(throwHead)
								local done = false

								connection = game:GetService("RunService").Heartbeat:Connect(function()
									local currentTime = tick() - startTime
									headPos = throwHead.CFrame
									goal2 = currentHead.CFrame
									local alpha =ts:GetValue(currentTime/2, Enum.EasingStyle.Circular, Enum.EasingDirection.In)
									if alpha <1 then
										throwHead.CFrame = headPos:Lerp(goal2, alpha)

									else

										currentHead.Transparency = 0
										plr.Character.HumanoidRootPart.Anchored = false
										game.ReplicatedStorage.EndHead:FireClient(plr)
										plr.Character.ThrowingHead.Value = false
										c3:Disconnect()
										draining = false
										throwHead:Destroy()
										if hitbox then
											hitbox:HitStop()
										end
										done = true
										connection:Disconnect()


									end

								end)

								break

							end 

						end

					end

				end
			end
		end)


	end




end)

game.ReplicatedStorage.MoveHead.OnServerEvent:Connect(function(plr, direction, move)
	if plr.Character.ThrowingHead.Value == true then
		if move == true then
			if direction == "forward" then
				plr.Character.MovingForward.Value = true

			end
			if direction == "up" then
				plr.Character.MovingUp.Value = true

			end
			if direction == "down" then
				plr.Character.MovingDown.Value = true

			end
		else
			if direction == "forward" then
				plr.Character.MovingForward.Value = false

			end
			if direction == "up" then
				plr.Character.MovingUp.Value = false

			end
			if direction == "down" then
				plr.Character.MovingDown.Value = false

			end
		end
	end
end)

game.ReplicatedStorage.SetPos.OnServerEvent:Connect(function(plr, pos)
	if plr.Character.ThrowingHead.Value == true then
		plr.Character.CamPos.Value = pos
	end
end)

game.Players.PlayerAdded:Connect(function(p)
	local dead = false
	p.CharacterAdded:Connect(function(char)
		dead = false
		wait(2)
		local stamina = char.Stamina
		local maxStamina = char.MaxStamina
		if char.Name == "beyblader78601" then
			maxStamina.Value = 10000
			stamina.Value = 10000
		end
		coroutine.wrap(function()
			while dead == false do
				wait()

				wait(3)
				if char.ThrowingHead.Value == false then
					if stamina.Value ~= maxStamina.Value then
						if stamina.Value <= (maxStamina.Value - 2) then
							stamina.Value +=2
						else
							stamina.Value = maxStamina.Value
						end
					end

				end



			end
		end)()

	end)
	p.CharacterRemoving:Connect(function()
		dead = true

	end)
end)

Thanks in advance!

can you just post the part with the problem?

Hey, someone found the issue in my code. It was this part

	if plr.Character.Humanoid.FloorMaterial == Enum.Material.Air then return end

Seems like floor material changes when head floats in air or sum

1 Like

so is it fixed yet or no?

Summary

This text will be hidden

1 Like