Humanoid state does Isn't changing when tool hits player

I’m trying to make a bat that can ragdoll and knock people back, this is the server script for the bat. The ragdoll works, but for some odd reason the humanoid state will not change. If anyone can look at my code and explain my mistake I would be very grateful, I apologize for messy code.

local tool = script.Parent
local HitboxEnabled = false

-- Cooldowns
local cooldown = false
local HitboxCooldown = false

-- Functions
local function hitbox()
	wait(.27)
	tool.Handle.Swing:Play()
	HitboxEnabled = true
	wait(.5)
	HitboxEnabled = false
end

local function knockback(pChar, eChar)
	if pChar and eChar then
		local pHrp = pChar:FindFirstChild("HumanoidRootPart")
		local eHrp = eChar:FindFirstChild("HumanoidRootPart")
		if pHrp and eHrp then
			local dir = (eHrp.Position - pHrp.Position).Unit
			local att = Instance.new("Attachment", eHrp)
			local force = Instance.new("VectorForce", eHrp)
			force.Attachment0 = att
			force.Force = (dir + Vector3.new(0, 1, 0)).Unit * 7000
			force.RelativeTo = Enum.ActuatorRelativeTo.World
			wait(.1)
			force:Destroy()
		end
	end
end

local function ragdoll(target)
	print(target)
	target:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Physics)
	for index,joint in pairs(target:GetDescendants()) do
		if joint:IsA("Motor6D") and target.Humanoid.Health > 0 then
			local socket = Instance.new("BallSocketConstraint")
			local a1 = Instance.new("Attachment")
			local a2 = Instance.new("Attachment")
			a1.Parent = joint.Part0
			a2.Parent = joint.Part1
			socket.Parent = joint.Parent
			socket.Attachment0 = a1
			socket.Attachment1 = a2
			a1.CFrame = joint.C0
			a2.CFrame = joint.C1
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true
			joint.Enabled = false
			target.Humanoid.RequiresNeck = false
		end
	end
	
	wait(2)
	target:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.GettingUp)
	for index, v in pairs(target:GetDescendants()) do
		if v:IsA("Motor6D") and target.Humanoid.Health > 0 then
			v.Enabled = true
		end
		if v:IsA("Attachment") and target.Humanoid.Health > 0 then
			v:Destroy()
		end
		if v:IsA("BallSocketConstraint") and target.Humanoid.Health > 0 then
			v:Destroy()
		end

		target.Humanoid.RequiresNeck = true
	end
end

-- Attack
tool.Handle.Touched:Connect(function(touched)

	if HitboxEnabled == true and HitboxCooldown == false then
		if touched.Parent:FindFirstChild("Humanoid") then
			if touched.Parent ~= tool.Parent then
				print(touched)
				HitboxCooldown = true
				tool.Handle.Impact:Play()
				touched.Parent:WaitForChild("Humanoid"):TakeDamage(13)
				local player = tool.Parent
			
				knockback(player, touched.Parent)
				ragdoll(touched.Parent)
				wait(.5)
				HitboxCooldown = false
			end 
		end
	end
end)

tool.Hitbox.OnServerEvent:Connect(function()
	hitbox()
end)

ChangeState() can only fire if the servers owns the player.

My mistake, thank you for the info.

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