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

Player Message Brick?

Asked by 9 years ago

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!

0
If your question was answered please click the 'accept answer' button! Perci1 4988 — 9y

2 answers

Log in to vote
0
Answered by
IcyEvil 260 Moderation Voter
9 years ago

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)
0
Oh...Well, thanks anyways :D bluecap67 0 — 9y
0
Who Down Voted me? My Attempt Works... IcyEvil 260 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
0
Sorry for my bad coding. But anyways, thanks a ton! The script works now and I'm very grateful :D bluecap67 0 — 9y

Answer this question