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

Message not being created on touch?

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.

1 comment

0

You did not add the Connection line.

4 answers

✓ Accepted answer

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.

1 comment

1

script.Parent.Touched:connect(onTouched) isn't calling the onTouched function. To call a function, you need two parentheses. Leave them out and you just get the function value -- which is what you want. You connect the Event to the Function, then roblox calls your function whenever the event fires. You just supply the value.

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)

5 comments

0

Ok, thanks!

0

Is this before or after "end"?

0

After "end". The calling of the function isn't going to be a part of the function itself :P

0

Ok thank you :)

0

You don't need an event to call a function.

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

1 comment

0

Thank you so much :) Expect more nooby questions in the future xD

script.Parent.Touched:connect(onTouched)

1 comment

1

For future reference, don't just state lines of code without explaining what you did, and why.

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