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

Message not being created on touch?

Asked by
AmVolt 60
9 years ago

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.

0
You did not add the Connection line. TheeDeathCaster 2368 — 9y

4 answers

Log in to vote
6
Answered by 9 years ago

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
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. Perci1 4988 — 9y
Ad
Log in to vote
2
Answered by 9 years ago

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)
0
Ok, thanks! AmVolt 60 — 9y
0
Is this before or after "end"? AmVolt 60 — 9y
0
After "end". The calling of the function isn't going to be a part of the function itself :P grasheeno 70 — 9y
0
Ok thank you :) AmVolt 60 — 9y
0
You don't need an event to call a function. DigitalVeer 1473 — 9y
Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

0
Thank you so much :) Expect more nooby questions in the future xD AmVolt 60 — 9y
Log in to vote
-4
Answered by 9 years ago
script.Parent.Touched:connect(onTouched)
1
For future reference, don't just state lines of code without explaining what you did, and why. Muoshuu 580 — 9y

Answer this question