Leaderstats not a valid member of Players?

Hello!
Currently I am trying to make a wall that when a player hits e, a GUI pops up, asking the player if they would like to unlock the next area for a certain amount of coins. That is as far as I got, I made a local script in the text button named “Buy” that will, when a player clicks it, make the wall invisible, same for the GUI. The players coins will go down, and the player will be allowed into the next area. However, after hours of trying to fix the error, I still get the same error in the Output.

11:54:19.881  leaderstats is not a valid member of Players "Players"  -  Client - LocalScript:5
  11:54:19.881  Stack Begin  -  Studio
  11:54:19.881  Script 'Players.tkuski08.PlayerGui.WallPurchaces.Frame.RainForestWallFrame.Buy.LocalScript', Line 5  -  Studio - LocalScript:5
  11:54:19.882  Stack End

Here is the script I am using

script.Parent.MouseButton1Click:Connect(function(player)

	if game.Players.leaderstats.Coins.Value >= game.Workspace.FirstWall.CostValue.Value then --"CostValue" is the IntValue
		game.player.leaderstats.Coins.Value = game.player.leaderstats.Coins.Value - game.Workspace.FirstWall.CostValue.Value
		
		game.Workspace.FirstWall.Transparency = 1

		game.Workspace.FirstWall.CanCollide = false

		game.Workspace.FirstWall.WhereProxIs.CanCollide = false

		script.Parent.Parent.Visible = false

		script.Parent.Parent.Parent.Visible = false



	end
end)

I had asked a friend of mine for help and we tried several different things, this script is with the IntValue that I added to the part but even without it, I get the same error.
I still have a lot to learn and I’m pretty sure this is an easy fix and I’m just thinking too hard. If anyone could help that would be great. Thanks :slight_smile:

You are not specifying the player while checking the coins in the leaderstats.

Your fixed code:

script.Parent.MouseButton1Click:Connect(function(player)

	if game.Players.player.leaderstats.Coins.Value >= game.Workspace.FirstWall.CostValue.Value then --"CostValue" is the IntValue
		game.Players.player.leaderstats.Coins.Value = game.Players.player.leaderstats.Coins.Value - game.Workspace.FirstWall.CostValue.Value
		
		game.Workspace.FirstWall.Transparency = 1

		game.Workspace.FirstWall.CanCollide = false

		game.Workspace.FirstWall.WhereProxIs.CanCollide = false

		script.Parent.Parent.Visible = false

		script.Parent.Parent.Parent.Visible = false



	end
end)

Also, is this a local script or a server script?

it is a local script. So the can collide and transparency only applies to one player right?

Yes. Any modifications to workspace from the client are only displayed locally as workspace is replicated.

Okay if this is a local script, you dont need to get the player from the MouseButton1Click event. You can easily get it from Players.LocalPlayer.

Use this code for your script:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")
local FirstWall = workspace:WaitForChild("FirstWall")

script.Parent.MouseButton1Click:Connect(function()

	if leaderstats.Coins.Value >= FirstWall.CostValue.Value then --"CostValue" is the IntValue
		leaderstats.Coins.Value -= FirstWall.CostValue.Value
		
		FirstWall.Transparency = 1

		FirstWall.CanCollide = false

		FirstWall.WhereProxIs.CanCollide = false

		script.Parent.Parent.Visible = false

		script.Parent.Parent.Parent.Visible = false
	end
end)

Yes but you can’t get the player from the mouse event. It returns the x and y cords of where the mouse clicked.

thank you, that worked, but i have a gui that displays when coins are taken away or added, for this it was taken away but displayed “+ -12500”

Just listen for the Value.Changed event and update the gui to the changed number or string.

I believe that is because you are subtracting a big number from the coins (from CostValue). Maybe the value for CostValue is too much?

Read the code, it specifically does not allow a player to pay if they don’t have enough coins.

it subtracts the value but + - pops up instead of just -

It appears this has something to do with a different script. Could you show the code of the script that displays the amount of coins you have?

local plr = game.Players.LocalPlayer
local ms = plr:GetMouse()
local stat = plr:WaitForChild("leaderstats")["Coins"]
local val = plr:WaitForChild("leaderstats")["Coins"].Value

stat.Changed:Connect(function()
	local text = script.Parent.PopUp.TextLabel:Clone()
	text.Parent = script.Parent.PopUp
	text.Name = "+".. tostring(stat.Value-val)
	text.Text = "+".. tostring(stat.Value-val)
	text.Rotation = math.random(-20,20)
	text.Visible = true
	local pos = UDim2.new(math.random(0,1000)/1000, 0,0.924, 0)
	text.Position = pos
	text:TweenPosition(pos-UDim2.new(0,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,1)
	wait(1)
	val = stat.Value
	text:Destroy()
end)

This is the script, I believe a friend of mine made it when we started this game a while ago

(local script in StarterGUI)

This script seems a bit messed up.

Try using this code for that script:

local Players = game:GetService("Players")
local plr = Players.LocalPlayer

local leaderstats = plr:WaitForChild("leaderstats")
local CoinsValue = leaderstats:WaitForChild("Coins")

CoinsValue.Changed:Connect(function()
	local text = script.Parent.PopUp.TextLabel:Clone()
	text.Parent = script.Parent.PopUp
	text.Name = "+"..CoinsValue.Name
	text.Text = "+".. tostring(CoinsValue.Value)
	text.Rotation = math.random(-20,20)
	text.Visible = true
	local pos = UDim2.new(math.random(0,1000)/1000, 0,0.924, 0)
	text.Position = pos
	text:TweenPosition(pos-UDim2.new(0,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,1)
	wait(1)
	text:Destroy()
end)

also when you leave the game, the coin value doesnt save…im guessing that is because the script doesnt have datastore?

ill try it

what is the variable for stat?

I forgot to change that. I have edited the code. Copy it again

it now says - and still takes away the right amount but the gui displays over 100k coins were taken

I believe you should make a different topic about this. The main issue was that you were receiving that error Leaderstats not a valid member of Players, which has now been solved. This is a different issue you are dealing with, and so I suggest you make a different topic. I can help you with this other issue, but it may just be too much for just 1 topic.

Okay, sorry for the trouble, thank you for helping me with the other issue, I will create a new topic on the new issue. (:

1 Like