Getting player MouseButton1Click screen gui

I am making a store screenGUI. I am trying to get the Player from a MouseButton1Click on a textButton so that I can make sure it has enough points and then subtract those points. Here is the code: (Top is what I have now bottom is how I want to get the points from leaderboard)

local sP = script.Parent

–Text colors
local yellow = Color3.fromRGB(245, 184, 41)
local grey = Color3.fromRGB(145, 145, 145)

–Lock, Unlock and Own item
local function updateItem(item, itemID, status)

local parentFrame = item

if status == "Owned" or status == "Locked" then
	parentFrame.BuyButton.TextColor3 = grey
	parentFrame.Info.TextColor3 = grey
	parentFrame.Title.TextColor3 = grey
	parentFrame.Pic.Image = parentFrame.Pic.Unsaturated.Value
	if status == "Owned" then
		parentFrame.BuyButton.Text = "Owned"
		parentFrame.isOwned.Value = true
		parentFrame.BuyButton.Text = "Locked"
		parentFrame.isLocked.Value = true
	parentFrame.BuyButton.TextColor3 = yellow
	parentFrame.Info.TextColor3 = yellow
	parentFrame.Title.TextColor3 = yellow
	parentFrame.Pic.Image = parentFrame.Pic.Saturated.Value
	parentFrame.BuyButton.Text = parentFrame.Cost.Value .. "pts"
	parentFrame.isLocked.Value = false


local function doPurchase(button)
local parentFrame = button.Parent

local isOwned = parentFrame.isOwned.Value
local isLocked = parentFrame.isLocked.Value
if isOwned ~= true or isLocked ~= true then
	updateItem(parentFrame, parentFrame.ItemID.Value, "Owned")
	--Unlocks upgrade
	--if parentFrame.isItem.Value == true then
	--print(parentFrame.ItemID.Value + 4 .. " item unlocked")	


for i, button in ipairs (sP.Store:GetDescendants()) do

if button.ClassName == 'TextButton' and button.Name == "BuyButton" then

	button.MouseButton1Click:Connect(function() -- Give it an anonymous function
		doPurchase(button) -- Fire the function with the button parameter


–Below is how I would like to get the points from leaderstats once I have the player

local player = game:GetService(“Players”):GetPlayerFromCharacter(Something idk)

local leaderstats = player.leaderstats

local pointStat = leaderstats and leaderstats:FindFirstChild(“Points”)

if pointStat.Value >= Cost then

pointStat.Value = pointStat.Value - Cost


I presume this is a local script? You can easily get the player via game:GetService("Players").LocalPlayer

1 Like

Yes it is a local script, but does that get it when it is clicked?

No, that gets it at the beginning of the game, when the client joins, if it’s outside of an event that is. You don’t really need to get it when it is clicked, just get it at anytime and do changes when needed

Ok so I will always have the player and the local script makes it like its separate for each player right?

LocalPlayer returns the client of each player, meaning yes each LocalPlayer will be different

1 Like

Ok, thankyou everything is working as intended! :grin:

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like