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

onTouch script help?

I have a part that when it touches another part or object it explodes but the explosion happens in the middle of the base plate and not on the part, help!

function onTouch(hit)
	local f=Instance.new("Explosion")
	f.Parent=script.Parent
	f.BlastPressure=500
	f.BlastRadius=500
end

script.Parent.Touched:connect(onTouch)

2 answers

✓ Accepted answer

Simple, just set the position of the explosion to the part's position.

f.Position=script.Parent.Position

Full script:

function onTouch() --We won't need any arguments here. Just making an explosion.
	local f=Instance.new("Explosion", script.Parent) --A shortcut to setting the explosion's Parent.
	f.BlastPressure=500
	f.BlastRadius=500
	f.Position=script.Parent.Position
end

script.Parent.Touched:connect(onTouch)

1 comment

0

Thanks so much!

The above persons script makes sense since the explosion will occur at the scripts parents position, but they still need debounce.

--First of all, debounce is just a variable, it's just a name for how we're using the variable.
--Debounce doesn't need to be named "debounce" it could be named anything!
--Ex: "thisisanokayname" "db" "wow"
--I'm going to use "db"

db = true -- make db true
function onTouch()
    if db then -- if db is true then continue with the script
    db = false -- set db to false so if the function is called before the function runs through all the way, it stops at the previous line
    local f=Instance.new("Explosion", script.Parent)
    f.BlastPressure=500
    f.BlastRadius=500
    f.Position=script.Parent.Position
    wait(.5)
    db = true -- now the function can run again because db = true
    end
end

script.Parent.Touched:connect(onTouch)

4 comments

0

I will never get debounce, it's too confusing...

1

basically the function can't run again until the last time it ran it has ran all the way through .. give me a moment and i'll break it down for you.

0

Add "end" on line 11, and maybe wait a bit before setting debounce back to true.

1

I saw the missing end thank you. I never use db so I forgot that we should probably wait.

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