Other function running after the function ended

2 blocks on folder

1 block on folder


Hello Developers!
as you can see in the first video (2 blocks on folder) it also slices/ runned the other part i didn’t touch. i tried using Debounce but it just continues slicing the other part if the debounce ended, i also tried using Disconnection as you can see in the recent script but this doesn’t work, i also tried 1 by 1 script but all of the script doesn’t work. I’ve been trying to fix this, this whole day but i can’t. i really appreciate your help, thanks!


Script

for _, objects in pairs(script.Parent.Based:GetChildren()) do
	warn('running')
	if objects:FindFirstChild('Slice')  then
		local connect
		connect = objects.Slice.Touched:Connect(function()
			print('start') 
		end)
		repeat wait()
		until game.Workspace:ArePartsTouchingOthers({game.Workspace.SaberBlue,objects.Slice},0)
		local var = game.ReplicatedStorage:FindFirstChild(objects.Name):Clone()
		var.Parent = workspace
		var.weldblock.CFrame = CFrame.new(objects.Position)
		var.weldblock.Anchored = false
		local audio = script.audio:Clone()
		audio.Parent = workspace.sound
		audio:Play()
		connect:Disconnect()
		wait(0.05)
		objects:Destroy()
	end
end

Are you putting the hitbox on the boxes? isn’t that a bad idea compared to just putting it on the sword?, anyways why not just use touched for the blade?

Well the hitbox module i installed to my game. I placed it on my sword because as i can see on those videos, it’s rarely on the tool or ye the sword so i put it there. But I’ll try that tomorrow. I can’t test it rn because it’s 10pm.

Any answers incase this doesn’t work?

I’m quite confused of the code because i’ve never used ArePartsTouchingOthers seems unefficient to what you’re trying to do, I recommend trying out Touched I think it’s best for what you’re trying to do instead of looping if something is touching that part. What you can do with Touched is as simple as this.

Blade.Touched:Connect(function(TouchedPart)
    if TouchedPart and TouchedPart.Parent then
        if TouchedPart.Parent:FindFirstChild("Something to Distinguish it as a hittable box.") then
            print(TouchedPart.Parent.Name, "Was hit!")
            BreakBlockDoSomethingHere()
        end
    end
end)
--There's alot of ways to distinguish an Object, we can either use Tagging(Collection Service), putting an Instance inside it or Using attributes, in my example I used an Instance.

PS: Make sure to add Sanity checks because touched can still be exploited on the client even if you attach it on the server side, as the Blade's NetworkOwnership will be on the client thus being exploitable if there’s no sanity checks.

1 Like

You could combat this with a simple task.spawn(), which will allow the thread to run immediately. You can do this such thing like the example below:

for _, objects in pairs(script.Parent.Based:GetChildren()) do
	warn('running')
	if objects:FindFirstChild('Slice')  then
		local connect
		task.spawn(function()
            connect = objects.Slice.Touched:Connect(function()
			print('start') 
            end)
		end)

		repeat 
            task.wait()
		until game.Workspace:ArePartsTouchingOthers({game.Workspace.SaberBlue,objects.Slice},0)

		local var = game.ReplicatedStorage:FindFirstChild(objects.Name):Clone()
		var.Parent = workspace
		var.weldblock.CFrame = CFrame.new(objects.Position)
		var.weldblock.Anchored = false

		local audio = script.audio:Clone()
		audio.Parent = workspace.sound
		audio:Play()

		connect:Disconnect()
		task.wait(0.05)
		objects:Destroy()
	end
end

(Do keep in mind this may not work (I haven’t tested it yet), but this would be a step in the right direction.)

Thanks, Cloud. :slightly_smiling_face:

1 Like

That was my first script but if the 2 parts (saber and the cube) touch the function doesn’t run. It only runs when my character touched the part

The Touched RBXScriptSignal object only fires for BaseParts which have their “CanTouch” and “CanCollide” properties enabled.

@Forummer’s answer might be the issue you’re facing you can use Region3 or OverlapParams.

OverlapParams

PS: This will require you to use looping.