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 10 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?

message = Instance.New("Message")
message.Text = messgeText
messageText = "Hello"


function onTouched(hit)

local h = hit.Parent:findFirstChild("Humanoid")

if h ~= nil then

message.Parent = Game.Workspace.Part
wait(5)


end
end
script.Parent.Touched:connect(onTouched) 

OnTouch(Game.Workspace.Part)

4 answers

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

Here is how I would do it:

local msgText = "Hi there. You should probably change this."

script.Parent.Touched:connect(function(hit)
    local findhum = hit.Parent:FindFirstChild("Humanoid")
    if findhum then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local findm = player.PlayerGui:findFirstChild("msg")
        if not findm then
            local msg = Instance.new("Message", player.PlayerGui)
            msg.Text = msgText
            msg.Name = "msg"
            wait(5)
            msg:remove()
        end
    end
end)
0
Mr1vgy it works! thank you so much awesometinyman 10 — 10y
Ad
Log in to vote
1
Answered by 10 years ago
function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") ~= nil then
        local message = Instance.New("Message", game.Workspace.Part)
        message.Text = "Hello"
        wait(5)
    end
end
script.Parent.Touched:connect(onTouched) 
Log in to vote
1
Answered by
yurhomi10 192
10 years ago

try this

touched = false -- debounce

script.Parent.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if touched == false then
        touched = true
        if h ~= nil then
            local message = Instance.new("Message")
            message.Text = "Hello"
            message.Parent = Workspace
            wait(5) -- edit
            message:Destroy()
        end
    touched = false
end)
0
yurhomi10 it works but i would like the message to go away after 5 seconds. awesometinyman 10 — 10y
0
try it now, i added a wait and destroy method yurhomi10 192 — 10y
Log in to vote
1
Answered by
war8989 35
10 years ago

Here's a simple way to do it:

debounce = false -- To keep the PlayerGui clean
duration = 5 -- How long the msg shows.
msgText = "Hello!" -- The text to be displayed

script.Parent.Touched:connect(function(hit)
    humanoid = hit.Parent:findFirstChild("Humanoid")
    if(humanoid ~= nil and debounce == false) then
        if(game.Players[hit.Parent.Name].PlayerGui:findFirstChild("Message") == nil) then
            debounce = true
            msg = Instance.new("Message", game.Players[hit.Parent.Name].PlayerGui)
            msg.Text = msgText
            wait(duration)
            msg:remove()
            debounce = false
        end
    end
end)

Answer this question