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

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

Asked by 4 years ago

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

Log in to vote
2
Answered by
crywink 419 Moderation Voter
4 years ago

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.

0
Remarkable answer! maxpax2009 340 — 4y
0
Sorry, that didn't work. Would it affect the script to know that it's not a LocalScript? JB_SuperGamer 165 — 4y
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. crywink 419 — 4y
0
A good way to do this is utilization of RemoteEvents and RemoteFunctions. crywink 419 — 4y
View all comments (2 more)
0
I don't know how to use RemoteEvents/RemoteFunctions :( JB_SuperGamer 165 — 4y
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. crywink 419 — 4y
Ad

Answer this question