Need help making a specific function for a part

So I want a part that will sense if anyone enters it instantly. If the part does not sense anyone in 1 second, the part dissapears. I’ve already tried “while true do” loops and such, but I also have a tiny pea brain, so I may have just been using it wrong. This is the code for the sensing bit:

for i, v in pairs(workspace:GetPartsInPart(x)) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then

						local x = Instance.new("IntValue", v.Parent)
						x.Name = "Hit"..Player.Name
						game.Debris:AddItem(x, 1)
						dashevent:FireServer(v)
					end
				end

Any ideas?

7 Likes

I would try collision detection and look for a HumanoidRootPart touching the part.
Or are your parts CanCollide = false?

Also, will all of these ‘appearing’ parts be in the same folder? So you can use one script to loop through them all?

The CanCollide is false. And also, the part spawns inside another Humanoid RootPart, which should not be sensed. It’s a hitbox.

I don’t get what you mean by ‘appearing’, but the “x” that you get the parts in is an instance.new.

If you will be adding parts to a folder dynamically in the game, you can use this…

local src = workspace:WaitForChild("PartsFolder")
src.ChildAdded:Connect(function(child)
	child:SetAttribute("Timer",1)
	child.Touched:Connect(function(part)
		if part.Name == "HumanoidRootPart" then
			child:SetAttribute("CharacterEntered",true)
		end
	end)
end)

local passedTime = 0
while true do
	for _,i in pairs(src:GetChildren()) do
		if not i:GetAttribute("CharacterEntered") then
			local timer = i:GetAttribute("Timer")
			timer = timer - passedTime
			if timer < 0 then
				i:Destroy()
			else
				i:SetAttribute("Timer",timer)
			end
		end 
	end
	pasedTime = task.wait()
end

or if all the parts are already in a folder and are static you can do

local src = workspace:WaitForChild("PartsFolder")
for _,child in pairs(src:GetChildren()) do
	child:SetAttribute("Timer",1)
	child.Touched:Connect(function(part)
		if part.Name == "HumanoidRootPart" then
			child:SetAttribute("CharacterEntered",true)
		end
	end)
end

local passedTime = 0
while true do
	for _,i in pairs(src:GetChildren()) do
		if not i:GetAttribute("CharacterEntered") then
			local timer = i:GetAttribute("Timer")
			timer = timer - passedTime
			if timer < 0 then
				i:Destroy()
			else
				i:SetAttribute("Timer",timer)
			end
		end 
	end
	pasedTime = task.wait()
end

or if you want an individual script per part, put this in each part

local src = script.Parent
local timer = 1
local characterEntered = false

src.Touched:Connect(function(part)
	if part.Name == "HumanoidRootPart" then
		characterEntered = true
	end
end)

while characterEntered == false do
	if timer < 0 then
		src:Destroy()
	end 
	timer = timer - task.wait()
end

Sorry, I posted all the above code before I saw your reply, let me re-evaluate

put this in the hitbox

local hitBox = script.Parent
local hrp = hitBox.Parent --I would advise against having part in part, but rather put the hitbox in the Character model instead
local timer = 1
local touched = false

function CheckTouched()
	for _, v in pairs(workspace:GetPartsInPart(hitBox)) do
		local player = game.Players:GetPlayerFromCharacter(v.Parent)
		if player and player~= game.Players.LocalPlayer then
			local x = Instance.new("IntValue", v.Parent)
			x.Name = "Hit"..Player.Name
			game.Debris:AddItem(x, 1)
			dashevent:FireServer(v)
			return true
		end
	end
	return false
end

while not touched do
	touched = CheckTouched()
	if timer < 0 then
		hitBox:Destroy()
	end
	timer = timer - task.wait()
end

You might have to make changes to your specific case, but this should be a pretty simple example

1 Like

So this is part of the script:

local function CheckTouched()
					for _, v in pairs(workspace:GetPartsInPart(x)) do
						local player = game.Players:GetPlayerFromCharacter(v.Parent)
						if player and player~= game.Players.LocalPlayer then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							dashevent:FireServer(v)
							return true
						end
					end
					return false
				end
				while not touched do
					touched = CheckTouched()
					if timer < 0 then
						x:Destroy()
					end
					timer = timer - task.wait(0.5)
				end
				vf:Destroy()
				playdashanim:Stop()

vf does not get destroyed the dash animation never stops. The loop won’t end, for some reason. Where should I put the break?

What are we checking for that might hit the hitbox?
Are we checking if another player hits it? Or will it be a weapon? or bullet? or npc?

Try adding this if statement around the call to the check touched function
image

What might hit the hitbox is the player creating it and an npc or other player. Also, for some reason, any time I try and do the function again, x does not spawn. This is my full code:

local function Dash()
	local character = Player.Character or Player.CharacterAdded:Wait()


	if character and not dashdebounce then
		dashdebounce = true
		local h = character.Humanoid
		local hrp = character.HumanoidRootPart
		local dashanim = Instance.new("Animation")
		dashanim.AnimationId = 'rbxassetid://14512159842'
		local playdashanim = h:LoadAnimation(dashanim)

		local dashdirection = nil
		local movedirection = h.MoveDirection
		local lv = hrp.CFrame.LookVector

		local isOnGround = h.FloorMaterial ~= Enum.Material.Air and h.FloorMaterial ~= Enum.Material.Water

		if isOnGround then
			if movedirection == Vector3.new(0,0,0) then
				dashdirection = hrp.Position + Vector3.new(lv.X, 0, lv.Z)
			else
				dashdirection = hrp.Position + Vector3.new(movedirection.X, 0, movedirection.Z)
			end
			
			local bg = Instance.new("BodyGyro")
			bg.Parent = hrp
			bg.D = 0
			bg.P = 500000
			bg.CFrame = CFrame.lookAt(hrp.Position, dashdirection)

			local att = Instance.new("Attachment")
			att.Parent = hrp

			local vf = Instance.new("VectorForce")
			vf.Parent = hrp
			vf.Attachment0 = att
			
			PlayAnim:Stop()
			playhitanimation:Stop()
			if UIS:IsKeyDown(Enum.KeyCode.W) then
				debounce = true
				playdashanim:Play()
				vf.Force = Vector3.new(0,0,-20000)
				wait(0.25)
				vf:Destroy()
				playdashanim:Stop()
				local x = Instance.new("Part", workspace)
				x.Name = "Hitbox"
				x.BrickColor = BrickColor.new("Really red")
				x.Size = Vector3.new(4, 4, 7)
				x.CanCollide = false
				x.Transparency = 0.5
				x.Massless = true
				local w = Instance.new("Weld", x)
				w.Part0 = Character.HumanoidRootPart
				w.Part1 = x
				w.C1 = CFrame.new(0, 0, 2)
				local function CheckTouched() --This is the function
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							dashevent:FireServer(v)
						end
					end
					return false
				end
				while not touched do
					if touched == false then
						touched = CheckTouched()
					end
					if timer < 0 then
						x:Destroy()
						break
					end
					timer = timer - task.wait(0.1)
				end
				
			elseif UIS:IsKeyDown(Enum.KeyCode.S) then
				vf.Force = Vector3.new(0,0,20000)
				wait(0.2)
				vf.Force = Vector3.new(0,0,-1000)
				wait(0.05)
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			elseif UIS:IsKeyDown(Enum.KeyCode.D) then
				local rightdashanim = Instance.new("Animation")
				rightdashanim.AnimationId = 'rbxassetid://14564814060'
				local playrightdashanim = Character.Humanoid:LoadAnimation(rightdashanim)
				playrightdashanim:Play()
				vf.Force = Vector3.new(20000,0,0)
				wait(0.2)
				vf.Force = Vector3.new(-1000,0,0)
				wait(0.05)
				playrightdashanim:Stop()
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			elseif UIS:IsKeyDown(Enum.KeyCode.A) then
				local leftdashanim = Instance.new("Animation")
				leftdashanim.AnimationId = 'rbxassetid://14565367605'
				local playleftdashanim = Character.Humanoid:LoadAnimation(leftdashanim)
				playleftdashanim:Play()
				vf.Force = Vector3.new(-20000,0,0)
				wait(0.2)
				vf.Force = Vector3.new(1000,0,0)
				wait(0.05)
				playleftdashanim:Stop()
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			else
				debounce = true
				playdashanim:Play()
				vf.Force = Vector3.new(0,0,-20000)
				local x = Instance.new("Part", workspace)
				x.Name = "Hitbox"
				x.BrickColor = BrickColor.new("Really red")
				x.Size = Vector3.new(4, 4, 7)
				x.CanCollide = false
				x.Transparency = 0.5
				x.Massless = true
				local w = Instance.new("Weld", x)
				w.Part0 = Character.HumanoidRootPart
				w.Part1 = x
				w.C1 = CFrame.new(0, 0, 2)
				local function CheckTouched()
					for _, v in pairs(workspace:GetPartsInPart(x)) do
						local player = game.Players:GetPlayerFromCharacter(v.Parent)
						if player and player~= game.Players.LocalPlayer then
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							dashevent:FireServer(v)
							return true
						end
					end
					return false
				end
				while not touched do
					touched = CheckTouched()
					if timer < 0 then
						x:Destroy()
						break
					end
					timer = timer - task.wait(0.1)
				end
				vf:Destroy()
				playdashanim:Stop()
			end

			
			
			
		else --Don't mind anything after this
			if movedirection == Vector3.new(0,0,0) then
				dashdirection = hrp.Position + Vector3.new(lv.X, 0, lv.Z)
			else
				dashdirection = hrp.Position + Vector3.new(movedirection.X, 0, movedirection.Z)
			end
			PlayAnim:Stop()
			playhitanimation:Stop()
			playdashanim:Play()
			local bg = Instance.new("BodyGyro")
			bg.Parent = hrp
			bg.D = 0
			bg.P = 500000
			bg.CFrame = CFrame.lookAt(hrp.Position, dashdirection)

			local att = Instance.new("Attachment")
			att.Parent = hrp

			local vf = Instance.new("VectorForce")
			vf.Parent = hrp
			vf.Attachment0 = att

			if UIS:IsKeyDown(Enum.KeyCode.W) then
				playdashanim:Play()
				vf.Force = Vector3.new(0,0,-20000)
				local x = Instance.new("Part", workspace)
				x.Name = "Hitbox"
				x.BrickColor = BrickColor.new("Really red")
				x.Size = Vector3.new(4, 4, 7)
				x.CanCollide = false
				x.Transparency = 0.5
				x.Massless = true
				local w = Instance.new("Weld", x)
				w.Part0 = Character.HumanoidRootPart
				w.Part1 = x
				w.C1 = CFrame.new(0, 0, 2)
				while task.wait(0.1) do
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then

							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							dashevent:FireServer(v)
						end
					end
					break --stops the loop
				end
				wait(duration)
				x:Destroy()
				playdashanim:Stop()
			elseif UIS:IsKeyDown(Enum.KeyCode.S) then
				vf.Force = Vector3.new(0,0,20000)
				wait(0.2)
				vf.Force = Vector3.new(0,0,-1000)
				wait(0.05)
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			elseif UIS:IsKeyDown(Enum.KeyCode.D) then
				vf.Force = Vector3.new(20000,0,0)
				wait(0.2)
				vf.Force = Vector3.new(-1000,0,0)
				wait(0.05)
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			elseif UIS:IsKeyDown(Enum.KeyCode.A) then
				vf.Force = Vector3.new(-20000,0,0)
				wait(0.2)
				vf.Force = Vector3.new(1000,0,0)
				wait(0.05)
				vf:Destroy()
				bg:Destroy()
				att:Destroy()
			end
		end

	end
end

You are not setting your dashdebounce to false again when the function finishes, or at least I didnt see it being set to false

the dash debounce is set to false in another function:

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == dashkey then
		if dashdebounce == false then
			sprintdebounce = true
			Character.Humanoid.WalkSpeed = 10
			Dash()
			sprintdebounce = false
			debounce =  false
			wait(3)
			dashdebounce = false
		end
	end
end)

I should mention, the velocity and dash animation are still there when I do it a second time, and they still stop, but x never spawns.

Set a breakpoint or 50 and see where the code is reaching, or some prints.
Is it even getting to where x = Instance.new?

Sorry for the late reply. This is my new code:

if UIS:IsKeyDown(Enum.KeyCode.W) then
				debounce = true
				playdashanim:Play()
				vf.Force = Vector3.new(0,0,-20000)
				wait(0.3)
				vf:Destroy()
				
				local x = Instance.new("Part", workspace)
				x.Name = "Hitbox"
				x.BrickColor = BrickColor.new("Really red")
				x.Size = Vector3.new(4, 4, 7)
				x.CanCollide = false
				x.Transparency = 0.5
				x.Massless = true
				local w = Instance.new("Weld", x)
				w.Part0 = Character.HumanoidRootPart
				w.Part1 = x
				w.C1 = CFrame.new(0, 0, 2)
				print("x creation")
				local function CheckTouched()
					for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							touched = true
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							dashevent:FireServer(v)
						end
					end
					return false
				end
				while not touched do
					print("not touched")
					if touched == false then
						touched = CheckTouched()
					end
					if timer < 0 then
						print("hitbox deletion")
						playdashanim:Stop()
						x:Destroy()
						break
					end
					timer = timer - task.wait(0.1)
				end

Strangely enough, despite x not being spawned, all of the prints work perfectly fine.

P.S. I tested it out, and for some reason x still FUNCTIONS the way it should (it still does the dash event when a target is in range) yet for some reason, it’s not showing up?

It it is showing its created, then it must be getting destroyed before you can see it. Are you resetting the ‘timer’ value? setting it back to 1, or how many seconds you want the hitbox to last?

So here’s what’s happening:
https://streamable.com/5v8nlo
I think your assumption is right, but the timer seems to only affect the first play through. I set the timer back to 1, should the task.wait() contain something aswell?

P.S. During the first play through, if a dummy manages to stay within the hitbox for the entire second (or longer) the dash event happens twice.

Edit: the link works now

From the video, it looks as though it is only working correctly when you are touching a dummy (other than the first time)

I would have to look in depth at the code and see what might be happening.

So I figured everything out. I forgot to reset the timer to 1 when the hitbox does not land. Also, the bug where it hit the target twice is fixed because the timer must be set to 0 when the hitbox hits. This explanation may be a little confusing, but it works, so doesn’t matter. Thank you for your help.

1 Like