How should I go about filtering out objects when using Instance.Touched:Wait()?

I’m currently making a custom-made version of the subspace tripmine that adheres to my game. I have a completed script that works completely fine, although the issue is that I would like to filter out objects that are not Players so that the subspace tripmine only explodes when a player touches it.

I’m using the Touched:Wait() event and I want to figure out how I can get the part that does end up touching the subspace tripmine so I can check if it is a character or not.

How would I do this?

ActivateBombRemote.OnServerEvent:Connect(function(user) -- This is the function that makes the bomb go off, which connects the ActivateBomb Remote Event to this script after it gets activated by the local script.
	local bombClone = bombHandle:Clone()
	bombClone.Parent = workspace.Bombs
	bombClone.CanCollide = true
	bombClone.CFrame = bombHandle.CFrame * CFrame.new(0, 2, 2)
	
	local tween = game.TweenService:Create(bombClone, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Transparency = 1
	})

	tween:Play()
	
	tickSound:Play()
	
	tickSound.Ended:Wait()
	
	bombClone.Touched:Wait()
	
	explode(bombClone, user)
end)
1 Like

you could probably use a part.Touched:Connect() and then check if any player.Character is an ancester of the part that connected (or check if theres a humanoid, if there are nothing else that uses humanoids)

1 Like
local Connection
ActivateBombRemote.OnServerEvent:Connect(function(user) -- This is the function that makes the bomb go off, which connects the ActivateBomb Remote Event to this script after it gets activated by the local script.
	local bombClone:BasePart? = bombHandle:Clone()
	bombClone.Parent = workspace.Bombs
	bombClone.CanCollide = true
	bombClone.CFrame = bombHandle.CFrame * CFrame.new(0, 2, 2)

	local tween = game.TweenService:Create(bombClone, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
		Transparency = 1
	})

	tween:Play()

	tickSound:Play()

	tickSound.Ended:Wait()

	Connection = bombClone.Touched:Connect(function(Hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
		if Player and tostring(Player) == tostring(user) then
			explode(bombClone, user)
			Connection:Disconnect() -- Assuming bombClone gets destroyed, this isn't necessary as destroying ANY instance also DROPS any events.
		end
	end)
end)
3 Likes

This ended up working, but this is the first time that I’ve seen something like this in any sort of code.

If you have the time, can you explain how this works and what connections are?

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