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?
function message(m) ---------------------------------------------------------------\\ m.Text = "How do I make you pop up once?" wait(5) ---------------------------------------------------------------// -- Color codes: -- (1,0,0) is Red -- (0,1,0) is Green -- (0,0,1) is Blue -- (1,1,1) is White -- And you can also mix colors. (1,0,1) is Purple for example -- For more settings, read the green text around the section below end function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then local player = game.Players:FindFirstChild(hit.Parent.Name) if player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui") == nil then sg = Instance.new("ScreenGui") sg.Parent = player:FindFirstChild("PlayerGui") end if player.PlayerGui.ScreenGui:FindFirstChild("MessageBox") == nil then -- In this section below, you can edit how the text box looks like local f = Instance.new("Frame") f.Name = "MessageBox" f.Position = UDim2.new(0.3, 0, 0.1, 0) f.Size = UDim2.new(0.4, 0, 0, 0) -- If Text box needs to be bigger, simply change "0.4" f.Style = "RobloxRound" -- Styles: "ChatBlue", "Chat Red", "Chat Green" f.Parent = player.PlayerGui:FindFirstChild("ScreenGui") local m = Instance.new("TextLabel") m.Position = UDim2.new(0.5, 0, 0.5, 500) m.FontSize = "Size14" -- Font Sizes: 10, 11, 12, 14, 18, 24 m.TextColor3 = Color3.new(1,1,1) -- Colors: (1,0,0) = Red, (0,1,0) = Green, (0,0,1) = Blue m.Parent = f message(m) f:Destroy() end end end end script.Parent.Touched:connect(onTouch)
Put a variable at the top like this:
local activated = false function message(m) if activate == false then activated = true m.Text = "How do I make you pop up once?" wait(5) end 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:
local connection -- define the variable before the function, because the function needs to be able to access it local activated = false -- if you need to know if the part was touched for whatever reason local function onTouch(hit) if whatever then if blahBlahBlah then -- stuff goes here activated = true connection:Disconnect() end end end 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:
function message(m) ---------------------------------------------------------------\\ m.Text = "How do I make you pop up once?" wait(5) ---------------------------------------------------------------// -- Color codes: -- (1,0,0) is Red -- (0,1,0) is Green -- (0,0,1) is Blue -- (1,1,1) is White -- And you can also mix colors. (1,0,1) is Purple for example -- For more settings, read the green text around the section below local AlreadyTouched = {} end function onTouch(hit) local AlreadyTouched = false--Automatically determines player has not touched for i, player in pairs(AlreadyTouched) do--Checks for player that already touched if hit.Parent.Name == player then AlreadyTouched = true--Player has already touched end end if AlreadyTouched == false then if hit.Parent:FindFirstChild("Humanoid") ~= nil then table.insert(AlreadyTouched,hit.Parent.Name)--Adds player to table if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then local player = game.Players:FindFirstChild(hit.Parent.Name) if player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui") == nil then sg = Instance.new("ScreenGui") sg.Parent = player:FindFirstChild("PlayerGui") end if player.PlayerGui.ScreenGui:FindFirstChild("MessageBox") == nil then -- In this section below, you can edit how the text box looks like local f = Instance.new("Frame") f.Name = "MessageBox" f.Position = UDim2.new(0.3, 0, 0.1, 0) f.Size = UDim2.new(0.4, 0, 0, 0) -- If Text box needs to be bigger, simply change "0.4" f.Style = "RobloxRound" -- Styles: "ChatBlue", "Chat Red", "Chat Green" f.Parent = player.PlayerGui:FindFirstChild("ScreenGui") local m = Instance.new("TextLabel") m.Position = UDim2.new(0.5, 0, 0.5, 500) m.FontSize = "Size14" -- Font Sizes: 10, 11, 12, 14, 18, 24 m.TextColor3 = Color3.new(1,1,1) -- Colors: (1,0,0) = Red, (0,1,0) = Green, (0,0,1) = Blue m.Parent = f message(m) f:Destroy() end end end end end script.Parent.Touched:connect(onTouch)
If you have any questions, comment below!
Best,
TheLastHabanero