Can you help me with a touch trigger?

There is a monster in my game. When player touches the part I want that the monster get destroyed.
But it doesn’t wait the trigger and destroys the monster.


local monster = workspace.Spider
local part = workspace.killmonster
local function destroy()
	if monster then 
		monster:Destroy()
	end
end
part.Touched:Connect(destroy)

Script is in the ServerScriptService.

Please be nice while helping and do not make fun of me for my terrible script😊

5 Likes

I think Scripts in ServerScriptService run instantly, so it doesn’t matter what function you have, it runs instantly. What you can do is place the script in the actual part which kills the monster

5 Likes

Yo, the problem is that you’re not checking if the thing that touched the part is a player or not. part.Touched is being Triggered but not by a player. When you do the part.Touched, it checks if the part is touching any instance in the workspace, I suggest you do this:

local monster = workspace.Spider
local part = workspace.killmonster

local function destroy(Hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	if monster then 
		monster:Destroy()
	end
end
part.Touched:Connect(destroy)

Or, you could do this:

local monster = workspace.Spider
local part = workspace.killmonster

local function destroy()
	if monster then 
		monster:Destroy()
	end
end
part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		destroy()
	else
		return
	end
end)
5 Likes

That would be pretty inefficient. Not recommended.

4 Likes

I think your problem is that, whenever the part touches anything the monster gets removed as long as it exists.

This should be the code:

local monster = workspace.Spider
local part = workspace.killmonster
local function destroy(hit)
	if hit == monster or hit.Parent == monster or hit.Parent.Parent == monster then 
		monster:Destroy()
	end
end
part.Touched:Connect(destroy)

You don’t need to connect the hit parameter at the end as it is automatically the first paramter in a .Touched event.

Also consider putting the script into the part that kills the player to keep oversight over your game and not have a messy game.

3 Likes

Hey, it worked with @Syntax_Hunter 's second script. And it destroyed the spider when part is touched.
Script worked with both script service and part’s script. Thank you!

3 Likes

You just gave me nice idea. Your script doesn’t work when player touched but it works perfect when spider touched the part and get destroyed. I’ll add this feature to my other monster. Thanks a lot =)

1 Like

Second script worked I’m happy. Thanks so much :smiley:

3 Likes

It worked as he said but I’ll think about ur opinion thank u :+1::+1::+1::blush:

2 Likes

The touching function really goes as simple as this:

workspace.Part.Touched:Connect(print)
--Prints the touching part

You can add cooldowns, filter down the part to see which one you want, the limitations are endless. There’s seriously nothing complicated to this.

If you want to find out more, you can go into this documentation page:

The first example is the Touched script.

For your case, you can try something like this:

--imagine the parts reference to your spider and monster.
local partA = workspace.PartA --Your spider
local partB = workspace.PartB --Your monster

partA.Touched:Connect(function(otherPart)
    if otherPart == partB then
        partA:Destroy()
    end
end)
3 Likes

Mark his reply as solution to help others having the same issue and also to give this man a better reputation.

1 Like

I read the event documentation like 4 months ago. Nice example, helps a lot :smiley:

2 Likes

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