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

Im having trouble with taking away leaderstats from a player?

function Give(player)
	local PlayerPoints = player.leaderstats.BrickClicks
	if PlayerPoints.Value < 19 then
	PlayerPoints.Value = PlayerPoints.Value - script.Parent.Cost.Value else
		print("You do not have enough brick clicks!")
	
	end
end
script.Parent.MouseButton1Click:Connect(Give)

The error i get is

  • Workspace.Part.SurfaceGui.TextButton.Script:2: attempt to index nil with 'leaderstats'

3 comments

1

Is this in a localscript or in a serverscript? MouseButton1Click won't run if it's a serverscript, and you can't change leaderstats with a localscript

0

im using a serverscript, so what should I connect it to>

0

Use a LocalScript with a RemoteEvent. Never use ServerScripts for gui based things.

2 answers

Since you're using MouseButton1Click, this is a localscript and the player can be found using game.Players.LocalPlayer.

player cannot be the parameter of the function because MouseButton1Click does not pass that parameter.

Also, as a side note, because this is a localscript, you can change the leaderstats, but only the local player will see the change and it will not take effect in the place. For it to take effect you need to use RemoteEvents / RemoteFunctions to communicate between a Localscript and server script.

4 comments

0

im not using a local script im using a regular script

0

Then as friendsgalore said, it won't detect the click when it's in a script

0

that's not true

0

Ran a test and Raccoonyz is correct that MouseButton1Click works in Server Scripts, I just didn't realize it because best practice is to put it in Local Scripts.

From looking at MouseButton1Click it appears that there are no values that the event provides. Thus saying, it does not provide a "player" object. You must get the player object yourself. Here is an example.

function Give()
    local player = script:FindFirstAncestorOfClass("Player")
    local PlayerPoints = player.leaderstats.BrickClicks
    if PlayerPoints.Value < 19 then
    PlayerPoints.Value = PlayerPoints.Value - script.Parent.Cost.Value else
        print("You do not have enough brick clicks!")

    end
end
script.Parent.MouseButton1Click:Connect(Give)

Note that the event DOES in fact run even though it's a server script.

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