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😊
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
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)
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.
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!
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 =)
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)