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

How can I identify a player who just clicked a Button?

I'm making a script that when a player presses a button, a Frame with the same name will turn green. It hasn't worked. Here's what I tried:

script.Parent.MouseButton1Click:Connect(function(click)
	script.Parent.Parent.PlayerStats[click].BackgroundColor3 = Color3.fromRGB(0, 255, 0)
	game.ReplicatedStorage.Values.PlayersReady.Value = game.ReplicatedStorage.Values.PlayersReady.Value +1
	script.Parent.Visible = false
end)

Please help! Thanks!

-JBennettChief9!

1 answer

Hey!

Since you are always going to be handling any user input on the client (LocalScript), we can use Players.LocalPlayer to find the local player, and in this situation, the player clicking the button. An example of this is shown below...

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.PlayerStats[Player].BackgroundColor3 = Color3.fromRGB(0, 255, 0)
    game.ReplicatedStorage.Values.PlayersReady.Value = game.ReplicatedStorage.Values.PlayersReady.Value +1
    script.Parent.Visible = false
end)

A tip for people that are relatively new: you can't use LocalPlayer on the server. This is because, well, code that runs on the server is ran on the server, and code that's ran on the client is ran on the client (your computer).

Hope this helped! If it did, please remember to upvote and mark as answered.

6 comments

0

Remarkable answer!

0

Sorry, that didn't work. Would it affect the script to know that it's not a LocalScript?

0

You should never be handling user input on the server! If you're using MouseButton1Click in a server script, you might want to change around how you're handling that.

0

A good way to do this is utilization of RemoteEvents and RemoteFunctions.

0

I don't know how to use RemoteEvents/RemoteFunctions :(

0

They're quite simple once you understand the basic principles of what they do and how they work. If you want to learn about how they, work I suggest taking a look at the Developer Hub.

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