Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.)

0
Remember, function names DO NOT AFFECT the function. All that matters is that you spell it right in your script. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

Ad

Answer this question