So I just started learning how to script and I am confused because I though that when I put
function onTouched() m = Instance.new("Message", game.Workspace) m.Text = "Ouch! That hurts!" wait(5) m:Remove() end
on a part and walked on it, then a message will appear. I made this script and put it in a Wedge part and when I walk on it, nothing happens. Help? Am I doing the wrong thing? Is there something I'm missing? Thanks.
Yes, you're almost there!
Remember, functions have to be CALLED in order for them to run.
Just saying:
function printa() print("@sera plz") end
Won't do ANYTHING.
To get the above function to work, you'd have to do;
function printa() print("@sera plz") end printa() --This tells the function to run
Now, let's do the same with yours:
function onTouched() m = Instance.new("Message", game.Workspace) m.Text = "Ouch! That hurts!" wait(5) m:Destroy() --[[Use :Destroy() rather than :Remove()]] end script.Parent.Touched:connect(onTouched)
script.Parent.Touched:connect(onTouched)
Although this line isn't exactly 'calling' the function, it does bind the function to be evaluated when the event fires.
In Roblox, a function can be called by an "event". The event for a part being touched is .Touched. At the end of your code, you would add:
script.Parent.Touched:connect(onTouched)
You have one error in this script. I'll try to explain it. Use this:
function onTouched() m = Instance.new("Message", game.Workspace) m.Text="Ouch! That hurts!" wait(5) m:remove() end script.Parent.Touched:connect(onTouched) --This connects the function to an actual event that fires when the part is touched. The event is called "Touched".
This should work now. You needed to connect the function to an event in order for it to work correctly, which is what I did on the last line. Hope I helped :P
script.Parent.Touched:connect(onTouched)