Part Doesn't Get Deleted

– Explaination
Hello, I’m working on a slapping game and I’ve ran into a major problem in the main gameplay side of the game. After slapping the opponent, I have it coded so that the IKControl using should be disabled so you cant slap the opponent till it’s your turn again.

– The Problem
When done with your round the part that I use for the IKControl doesn’t get deleted it just stays there and I’m confused why this is happening. How could I fix this?

– Code

MainEvent.OnServerEvent:Connect(function(LocalPlr,Action,Mouse)
	-- Constants / Character --
	local Character = LocalPlr.Character
	local Humanoid = Character:FindFirstChild('Humanoid')
	local HBFolder = Character:FindFirstChild('Hitboxes')
	local HandHitbox = HBFolder:FindFirstChild('HandBox')
	local HeadHitbox = HBFolder:FindFirstChild('HeadBox')
	-- Constants / Values --
	local FollowPart = nil
	-- Check x3 --
	if Action == 'Slapping' then
		if FollowPart == nil then
			if not Character:FindFirstChild('FollowPart') then
				FollowPart = Instance.new('Part',Character) FollowPart.Name = 'FollowPart'
				local AlignPosition = Instance.new('AlignPosition',FollowPart)
				local Attachment = Instance.new('Attachment',FollowPart)
				-- Properties / Part --
				FollowPart.Size = Vector3.new(1,1,1)
				FollowPart.Anchored = false
				FollowPart.CanCollide = false
				FollowPart.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
				-- Properties / AlignPos --
				AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
				AlignPosition.Attachment0 = Attachment
				AlignPosition.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
				-- Update IKControls --
				Humanoid:FindFirstChild('ArmControl').Target = FollowPart
				Humanoid:FindFirstChild('TorsoControl').Target = FollowPart
			else
				FollowPart = Character:FindFirstChild('FollowPart')
				local AlignPosition = FollowPart:FindFirstChild('AlignPosition')
				-- Update IKControls --
				Humanoid:FindFirstChild('ArmControl').Target = FollowPart
				Humanoid:FindFirstChild('TorsoControl').Target = FollowPart
				-- While Statement --
				while Character:GetAttribute('Slapping',true) do
					RunService.Heartbeat:Wait()
					-- Update --
					AlignPosition.Position = Vector3.new(Mouse,FollowPart.Position.Y,FollowPart.Position.Z)
				end
			end
		end
		-- Check --
	elseif Action == 'GetSlapped' then
		-- Check --
		if FollowPart ~= nil then
			-- Remove FollowPart --
			if Character:FindFirstChild('FollowPart') then
				Character.FollowPart:Destroy()
				-- Update --
				FollowPart = nil
				-- Update IKControls --
				Humanoid:FindFirstChild('ArmControl').Target = nil
				Humanoid:FindFirstChild('TorsoControl').Target = nil
			end
		else
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = nil
			Humanoid:FindFirstChild('TorsoControl').Target = nil
		end
	end
end)
----

Every time the remote gets fired, the FollowPart value will refresh to nil, so you can do this and remove an unneccessary check if Action is GetSlapped

elseif Action == 'GetSlapped' then
		-- Check --
		-- Remove FollowPart --
		if Character:FindFirstChild('FollowPart') then
			Character.FollowPart:Destroy()
			-- Update --
			FollowPart = nil
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = nil
			Humanoid:FindFirstChild('TorsoControl').Target = nil
		else
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = nil
			Humanoid:FindFirstChild('TorsoControl').Target = nil
		end
	end


It still does the same thing

There’s no need to set FollowPart to nil once you’ve destroyed the part.

One thing I noticed is that you don’t immediately destroy the part once the slap has occured. Instead, try destroying the part after a certain delay once the slap is finished.

if not Character:FindFirstChild('FollowPart') then
				FollowPart = Instance.new('Part',Character) FollowPart.Name = 'FollowPart'
				local AlignPosition = Instance.new('AlignPosition',FollowPart)
				local Attachment = Instance.new('Attachment',FollowPart)
				-- Properties / Part --
				FollowPart.Size = Vector3.new(1,1,1)
				FollowPart.Anchored = false
				FollowPart.CanCollide = false
				FollowPart.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
				-- Properties / AlignPos --
				AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
				AlignPosition.Attachment0 = Attachment
				AlignPosition.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
				-- Update IKControls --
				Humanoid:FindFirstChild('ArmControl').Target = FollowPart
				Humanoid:FindFirstChild('TorsoControl').Target = FollowPart
				game:GetService("Debris"):AddItem(FollowPart, 1.3) -- Change 1.3 to the delay before the part being destroyed.
 			else
             --- Put rest here

Hope this helps!

This kinda just broke the entire thing

1 Like

Even if I just do this

It doesn’t change the IKControls Target to nil

I deleted some more unnecessary if statements, now it checks if the part is in the character

MainEvent.OnServerEvent:Connect(function(LocalPlr,Action,Mouse)
	-- Constants / Character --
	local Character = LocalPlr.Character
	local Humanoid = Character:FindFirstChild('Humanoid')
	local HBFolder = Character:FindFirstChild('Hitboxes')
	local HandHitbox = HBFolder:FindFirstChild('HandBox')
	local HeadHitbox = HBFolder:FindFirstChild('HeadBox')
	-- Constants / Values --
	-- Check x3 --
	if Action == 'Slapping' then
		if not Character:FindFirstChild('FollowPart') then
			local FollowPart = Instance.new('Part') 
			FollowPart.Name = 'FollowPart'

			local AlignPosition = Instance.new('AlignPosition',FollowPart)
			local Attachment = Instance.new('Attachment',FollowPart)
			-- Properties / Part --
			FollowPart.Size = Vector3.new(1,1,1)
			FollowPart.Anchored = false
			FollowPart.CanCollide = false
			FollowPart.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
			FollowPart.Parent = Character -- for optimization
			-- Properties / AlignPos --
			AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
			AlignPosition.Attachment0 = Attachment
			AlignPosition.Position = Vector3.new(Character.Head.Position.X,Character.Head.Position.Y + 2,Character.Head.Position.Z + 10)
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = FollowPart
			Humanoid:FindFirstChild('TorsoControl').Target = FollowPart
		else
			local FollowPart = Character:FindFirstChild('FollowPart')
			local AlignPosition = FollowPart:FindFirstChild('AlignPosition')
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = FollowPart
			Humanoid:FindFirstChild('TorsoControl').Target = FollowPart
			-- While Statement --
			while Character:GetAttribute('Slapping',true) do
				RunService.Heartbeat:Wait()
				-- Update --
				AlignPosition.Position = Vector3.new(Mouse,FollowPart.Position.Y,FollowPart.Position.Z)
			end
		end
		-- Check --
	elseif Action == 'GetSlapped' then
		-- Check --

		-- Remove FollowPart --
		if Character:FindFirstChild('FollowPart') then
			Character.FollowPart:Destroy()
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = nil
			Humanoid:FindFirstChild('TorsoControl').Target = nil
		else
			-- Update IKControls --
			Humanoid:FindFirstChild('ArmControl').Target = nil
			Humanoid:FindFirstChild('TorsoControl').Target = nil
		end
	end
end)
----

Try accessing IKControls directly without using FindFirstChild

2 Likes

making a target nill wont do anything just also do the target.parent = nill

is target a value? you can make the value nill or the parent nill or put it in its own script

image
It’s a IKControl the Target has to be a BasePart or a Part of some-sort to operate

Nope, that didn’t do anything either