Getting Player from Button Clicked with Local Script?

I had a quick question while working on my scripting world today. How would you find out what player clicked a button in a situation like shown below?

Okay, so I have a textButton with the following script as a localscript:

script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireServer(5647630275)
end)

And then I of course had the RemoteEvent named “RemoteEvent”

And then I had this script called “PlayAnimScript” that plays the animation, which had the following script:
local AnimPlaying = false game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,animationID) local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID="..animationID if AnimPlaying == false then local loadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation) loadedAnimation:Play() AnimPlaying=true wait(1) local headLifts = player.leaderstats.headLifts headLifts.Value = headLifts.Value+1 AnimPlaying=false end end)

And I was wondering how I would get this area to work:
local headLifts = player.leaderstats.headLifts headLifts.Value = headLifts.Value+1

How would I get the specific player who clicked the button in the first place?

All this script worked before adding the leaderstats area into the server script, which I’m just leaving in there for now because I know I’ll need it

2 Likes

Is this in a gui? if so can you say what type it is, billboard surface screen?

@jake_4543
I have a textButton that is communicating through a RemoteEvent to a server script.

This doesnt answer my question, is it in a billboard gui surface gui or screen gui?

oh sorry it’s a screengui that I was working with.

Any errors in your output window?

Edit: Also, the player who fired the RemoteEvent is the player that clicked the button.

RemoteEvent.OnServerEvent:Connect(function(player, animationID)
    -- the player argument is the player who clicked the button
end)

This is unrelated, but you can also use player.Character instead of this:

1 Like

@P1X3L_K1NG
There’s nothing showing in the scripts or appearing in script output during the game
:confused:

Edited:

Sorry I had a brainfart. On your OnServerEvent function, the first argument is always the player who fired the event.

1 Like

Wait now I’m confused should I use the script below?

the phrase “id” has an orange underline and would FireServer be another RemoteEvent?

Maybe try replacing your code with the following:

-- LocalScript inside of your button
local animationID = 5647630275
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=" .. animationID
local loadedAnimation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)

script.Parent.MouseButton1Click:Connect(function()
    animation:Play() -- animations played on client replicate to the server
end)

Replacing the script with what you wrote isn’t working, and I would still need it to add 1 to the leaderstats variable.

Whoops! My bad, I’ll include that too.

Are there any errors?

Humanoid is not a valid member of Model

I didn’t think it was possible to change leaderstats inside of localscripts.

What you could also do instead (and this would work much better anyways) would be to add an Animation object inside of the script with the ID pasted into it. Then, you can replace my code with this code:

-- LocalScript inside of your button, with an Animation object called "Animation" with your animation ID inside of it
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animation = humanoid:LoadAnimation(script.Animation)

script.Parent.MouseButton1Click:Connect(function()
    animation:Play()
    remoteEvent:FireServer()
end)

-- ServerScript inside of ServerScriptService
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(plr)
    plr.leaderstats.headLifts.Value += 1
end)

Also make sure to read the comments, as they will help you set this up properly. Let me know if there is a problem :slight_smile:

Simply local player = game.Players.LocalPlayer

Player.Character:WaitForChild(“Humanoid”)

@P1X3L_K1NG
I’m sort of confused with what you’re saying here. You’re saying to replace your code with your code? I’m not sure I fully understand.

@ShutzCh
Where would I put that? In the server or local script?

i think it can only work in local script

I meant for you to disregard my other script, and to try this script instead. Make sure to read the comments in the code so that you set the script up properly.