Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player touches block and a message appears?

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

✓ Accepted answer

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)

1 comment

0

Mr1vgy it works! thank you so much

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) 

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)

2 comments

0

yurhomi10 it works but i would like the message to go away after 5 seconds.

0

try it now, i added a wait and destroy method

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.