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

Simple scripting question?

Asked by
AmVolt 60
9 years ago

Okay so I'm just beginning so don't laugh :P I'm trying to make a script so when you touch a brick a message appears on the screen. This is what I have come up with.

function onTouched() m = Instance.new("Text" , game.Workspace) m.Text = "Thanks for the help xD" wait(5) m:destroy() end script.Parent.Touched:connect(onTouched)

This doesn't seem to work though when put inside a brick, do I need to add another instance or is the script not correct?

Also, for this script I used m= . Do I have to use m? Can I use whatever word or letter I want? Thank you :)

0
function onTouched() 2 3 m = Instance.new("Message", game.Workspace) 4 m.Text = "Ouch! That hurts!" 5 wait(5) 6 m:Remove() 7 end AmVolt 60 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

Some tips:

-For putting lua code on this site, use the right-most button (it says "Lua" on it). It'll show a bunch of ~s; put your code between the two lines of ~s.

-Have the Output window open so that you can see errors. You would then see that when your script runs, it gives the error 'Unable to create an Instance of type "Text"'.

-"m" is indeed a variable, which can be named almost anything (though you can't name it keywords (see the manual), and you shouldn't name it the same as a global function/library (ex "math" - or else you would no longer be able to use the functions within "math").

-The script in your main question doesn't work because you're trying to create an instance of "Text" (which doesn't exist). The script in your comment doesn't work because you didn't connect the event to your function (though you did that in the script in your main question).

-You should use "debounce" to prevent the function from firing multiple times simultaneously (unless you want any number of messages to appear simultaneously).

-"m" should be made local. See this thread for a very good explanation.

The full script, with debounce:

db = false --debounce
function onTouched()
    if db then return end --if we're already running the function, don't make another message
    db = true
    local m = Instance.new("Message", game.Workspace)
    m.Text = "Ouch! That hurts!"
    wait(5)
    m:Remove()
    db = false --this allows the next Touch of script.Parent to create another message
end
script.Parent.Touched:connect(onTouched)
0
Thank you so much. AmVolt 60 — 9y
Ad
Log in to vote
0
Answered by
AmVolt 60
9 years ago

And also another question, how do you put codes in Standard Lua form?


Answer this question