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)
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)
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)
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)