Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this explode without being touched?

I have two scripts, one that creates and adds the script to the part, and one that explodes the part on touched. Why does the part explode instantly until being removed?

Here is the first:

function createmine()
	local mine = Instance.new("Part")
	mine.Parent = workspace
	mine.Position = game.Workspace:FindFirstChild("master").Position
	mine.Name = "mine"
	mine.Size = game.Workspace:FindFirstChild("master").Size
end

function expscript()
	local mine = game.Workspace:FindFirstChild("mine")
	local script = game.ReplicatedStorage:FindFirstChild("explosion")
	script:Clone().Parent = mine
end

while true do
	wait(2)
if game.Workspace:FindFirstChild("mine") == nil then
	createmine()
	expscript()
end
wait(2)
end

And here is the one being added:

function OnTouched()
	local expl = Instance.new("Explosion")
	expl.Position = script.Parent.Position
	expl.Parent = script.Parent
	wait(1)
	script.Parent:Remove()
end

script.Parent.Touched:connect(OnTouched)

1 answer

✓ Accepted answer

The OnTouched() is Fired whenever the Mine touches ANY part, not just a Character. If you wan't it to be Character only, then you have to search something, that is unique only for a Character, which is Humanoid. This is how the script should look like:

function OnTouched(Part)
if Part.Parent:FindFirstChild("Humanoid") then
    local expl = Instance.new("Explosion")
    expl.Position = script.Parent.Position
    expl.Parent = script.Parent
    wait(1)
    script.Parent:Remove()
end
end

script.Parent.Touched:connect(OnTouched)

Hope it helps!

1 comment

0

Ah, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.