This question might be considered redundant, but it would help tons if you answer it.
I'm trying to script a brick that instances a message, except I don't want it in the Workspace - I only want the player who touched it to see it.
I already know that type of message has to be inside the Player of the character who touched it, but it never works out. Please help?
SCRIPT:
function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") local debounce = true if h~=nil and debounce == true then debounce = false local msg = Instance.new("Message") msg.Text = "Insert some weird message heeere" msg.Parent = game.Players[h.Parent.Name] wait(5) debounce = true msg:remove() end end script.Parent.Touched:connect(onTouched)
If you could help me, thanks!
Here is something where if you touch it it displays a message, I did not know how to make a message appear via only a player unless you create a Gui to insert into PlayerGui
script.Parent.Touched:connect(function(hit) if hit.Parent then local h = hit.Parent local debounce = true if h~=nil and debounce == true then debounce = false local msg = Instance.new("Message") msg.Text = "Insert some weird message heeere" msg.Parent = game.Workspace wait(5) debounce = true msg:Destroy() end end end)
The message will not be shown when parented inside a Player.
To make a message that only a single player can see, you must put it in their PlayerGui
.
There are some other problems, however.
Nothing is tabbed correctly; doesn't effect functionality but readability is arguably even more important.
You define debounce
inside the function, so every time the Touched event is fired, debounce
will simply equal true, because you set it to true inside the function before anything else.
You should use the second argument of Instance.new
to keep you code clean.
Use the GetPlayerFromCharacter
method to get the Player from a character. This also makes looking for a humanoid unnecessary, since we can just directly check if the person who touched the brick is a Player.
The remove
method simply sets an object's parent to nil, and is deprecated. Use Destroy
instead.
Here is my attempt at improving your code;
local debounce = true function onTouch(part) local plr = game.Players:GetPlayerFromCharacter(part.Parent) if plr ~= nil and debounce == true then debounce = false local msg = Instance.new("Message", plr:WaitForChild("PlayerGui")) msg.Text = "Hi" wait(5) debounce = true msg:Destroy() end end