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

How to make a onTouch script work when a model hits it?

So i am making a game that has a model touch a part, but it has a onTouch script inside that part and when the model hits it, nothing happens. I think this is because of the "(hit)" thing, but I am not completly sure.

function onTouched(hit)

script.Parent.Transparency = 1
script.Parent.Parent.Sound:Play()
end

(Sorry if my scripting isn't that long, also note I'm a beginner scripter so I don't know much yet.)

1 comment

0

Remember, function names DO NOT AFFECT the function. All that matters is that you spell it right in your script.

1 answer

First of all, you're not connecting the function to any event. An event in rblx.Lua is basically a way to trigger something. One thing that I see a lot of beginners do is think that the name of the function will trigger the function, which actually the event triggers it. So you should have:

function onTouched() -- removed the hit argument since it is pointless for now
	script.Parent.Transparency = 1
	script.Parent.Parent.Sound:Play()
end

script.Parent.Touched:Connect(onTouched) -- this line is what tells the function "onTouched" to run

The event "Touched" is a predefined event that triggers something whenever "script.Parent" in this case, is touched.

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