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

Player touches block and a message appears?

Asked by 11 years ago

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?

01message = Instance.New("Message")
02message.Text = messgeText
03messageText = "Hello"
04 
05 
06function onTouched(hit)
07 
08local h = hit.Parent:findFirstChild("Humanoid")
09 
10if h ~= nil then
11 
12message.Parent = Game.Workspace.Part
13wait(5)
14 
15 
16end
17end
18script.Parent.Touched:connect(onTouched)
19 
20OnTouch(Game.Workspace.Part)

4 answers

Log in to vote
1
Answered by
Mr1Vgy 30
11 years ago

Here is how I would do it:

01local msgText = "Hi there. You should probably change this."
02 
03script.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
16end)
0
Mr1vgy it works! thank you so much awesometinyman 10 — 11y
Ad
Log in to vote
1
Answered by 11 years ago
1function 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
7end
8script.Parent.Touched:connect(onTouched)
Log in to vote
1
Answered by
yurhomi10 192
11 years ago

try this

01touched = false -- debounce
02 
03script.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
15end)
0
yurhomi10 it works but i would like the message to go away after 5 seconds. awesometinyman 10 — 11y
0
try it now, i added a wait and destroy method yurhomi10 192 — 11y
Log in to vote
1
Answered by
war8989 35
11 years ago

Here's a simple way to do it:

01debounce = false -- To keep the PlayerGui clean
02duration = 5 -- How long the msg shows.
03msgText = "Hello!" -- The text to be displayed
04 
05script.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
17end)

Answer this question