Im trying to build a brick that if you touch it will send a message. It is not working here is my script. What do you i need to fix in the script?
01 | message = Instance.New( "Message" ) |
02 | message.Text = messgeText |
03 | messageText = "Hello" |
04 |
05 |
06 | function onTouched(hit) |
07 |
08 | local h = hit.Parent:findFirstChild( "Humanoid" ) |
09 |
10 | if h ~ = nil then |
11 |
12 | message.Parent = Game.Workspace.Part |
13 | wait( 5 ) |
14 |
15 |
16 | end |
17 | end |
18 | script.Parent.Touched:connect(onTouched) |
19 |
20 | OnTouch(Game.Workspace.Part) |
Here is how I would do it:
01 | local msgText = "Hi there. You should probably change this." |
02 |
03 | script.Parent.Touched:connect( function (hit) |
04 | local findhum = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if findhum then |
06 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
07 | local findm = player.PlayerGui:findFirstChild( "msg" ) |
08 | if not findm then |
09 | local msg = Instance.new( "Message" , player.PlayerGui) |
10 | msg.Text = msgText |
11 | msg.Name = "msg" |
12 | wait( 5 ) |
13 | msg:remove() |
14 | end |
15 | end |
16 | end ) |
1 | function onTouched(hit) |
2 | if hit.Parent:findFirstChild( "Humanoid" ) ~ = nil then |
3 | local message = Instance.New( "Message" , game.Workspace.Part) |
4 | message.Text = "Hello" |
5 | wait( 5 ) |
6 | end |
7 | end |
8 | script.Parent.Touched:connect(onTouched) |
try this
01 | touched = false -- debounce |
02 |
03 | script.Parent.Touched:connect( function (hit) |
04 | local h = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if touched = = false then |
06 | touched = true |
07 | if h ~ = nil then |
08 | local message = Instance.new( "Message" ) |
09 | message.Text = "Hello" |
10 | message.Parent = Workspace |
11 | wait( 5 ) -- edit |
12 | message:Destroy() |
13 | end |
14 | touched = false |
15 | end ) |
Here's a simple way to do it:
01 | debounce = false -- To keep the PlayerGui clean |
02 | duration = 5 -- How long the msg shows. |
03 | msgText = "Hello!" -- The text to be displayed |
04 |
05 | script.Parent.Touched:connect( function (hit) |
06 | humanoid = hit.Parent:findFirstChild( "Humanoid" ) |
07 | if (humanoid ~ = nil and debounce = = false ) then |
08 | if (game.Players [ hit.Parent.Name ] .PlayerGui:findFirstChild( "Message" ) = = nil ) then |
09 | debounce = true |
10 | msg = Instance.new( "Message" , game.Players [ hit.Parent.Name ] .PlayerGui) |
11 | msg.Text = msgText |
12 | wait(duration) |
13 | msg:remove() |
14 | debounce = false |
15 | end |
16 | end |
17 | end ) |