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

Error when message is only showing to the player who clicked the button?

Asked by 7 years ago

I am making a message that will only appear to the person who clicked the button but it won't work could someone help me fix it? Heres the code:

script.Parent.ClickDetector.mouseClick:connect(onClicked)

function onClicked()
    local message = Instance.new('Message', game.Players.LocalPlayer.PlayerGui) -- Insert a new message in the Workspace.
    message.Text = "Admins Only in this room" -- Set the text of the message
    wait(5)
    message:Destroy() -- Remove the message after 5 seconds.
end


1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

First of all, the event that fires from the ClickDetector when a player clicks on it is MouseClick, not mouseClick.

Secondly, at line 1, you can see that you've connected the event to the onClicked function, but the script doesn't know what onClicked is yet until it is defined line 3. So, that's another reason it will break.

Lastly, you used game.Players.LocalPlayer. LocalPlayer only works in LocalScripts run by the client, and not the ROBLOX game server. Instead, you can use an argument passed by the MouseClick of the ClickDetector like this:

function onClicked(playerWhoClicked)
    print(playerWhoClicked.Name.." clicked this!")
end
0
Thanks DevOverr 15 — 7y
Ad

Answer this question