I am trying to make this message script only pop up one time. It works when the player touches the block and a message pops up. When the message is over and the player touches the block again it shows pops up again which is not what I want. Any help?
01 | function message(m) |
02 |
03 |
04 |
05 | ---------------------------------------------------------------\\ |
06 |
07 |
08 | m.Text = "How do I make you pop up once?" |
09 | wait( 5 ) |
10 |
11 |
12 |
13 |
14 | ---------------------------------------------------------------// |
15 |
Put a variable at the top like this:
1 | local activated = false |
2 |
3 | function message(m) |
4 | if activate = = false then |
5 | activated = true |
6 | m.Text = "How do I make you pop up once?" |
7 | wait( 5 ) |
8 | end |
9 | end |
since you're connecting to an event, you can disconnect the function once you're done with RBXScriptConnection:Disconnect()
in your code, that would look something like this:
01 | local connection -- define the variable before the function, because the function needs to be able to access it |
02 | local activated = false -- if you need to know if the part was touched for whatever reason |
03 |
04 | local function onTouch(hit) |
05 | if whatever then |
06 | if blahBlahBlah then |
07 | -- stuff goes here |
08 | activated = true |
09 | connection:Disconnect() |
10 | end |
11 | end |
12 | end |
13 | connection = script.Parent.Touched:Connect(onTouch) -- when you Connect to an event, it returns an RBXScriptConnection which you can use to disconnect the function later |
also, make sure you put local
whenever you define a variable or function
why? it's good practice
why is it good practice? it's faster when you do that, also there's global environment blah blah blah whatever It's Not Good stuff but i'm not advanced enough to tell you why global environment is bad
Hello! I can see your issue! I'd suggest using tables to store players that have already touched the brick, like so:
01 | function message(m) |
02 |
03 |
04 |
05 | ---------------------------------------------------------------\\ |
06 |
07 |
08 | m.Text = "How do I make you pop up once?" |
09 | wait( 5 ) |
10 |
11 |
12 |
13 |
14 | ---------------------------------------------------------------// |
15 |
If you have any questions, comment below!
Best,
TheLastHabanero