Trouble getting leaderstats

I want to make a clicking gui for clicks

local button = script.Parent
local event = game.ReplicatedStorage.ClickEvent
local MarketPlaceService = game:GetService("MarketplaceService")
local GamePassID = 14905680
local player = game:GetService("Players").LocalPlayer
local clicks = player.leaderstats.Clicks
local userInput = game:GetService('UserInputService')


event.OnServerEvent:Connect(function()
	player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassID) then
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 9
	end
end)

Do you have any output errors?

image I think its not getting the player, so it cant get the leaderstats

Are you using a LocalScript inside of a GUI Instance?
If so, try using game.Players.LocalPlayer

This is in a server script. The local script is firing the event.

Just get the player from the serverscript then that’s it by adding player at the script.

Full Server script:

local button = script.Parent
local event = game.ReplicatedStorage.ClickEvent
local MarketPlaceService = game:GetService("MarketplaceService")
local GamePassID = 14905680
local clicks = player.leaderstats.Clicks
local userInput = game:GetService('UserInputService')


event.OnServerEvent:Connect(function(player)
	player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassID) then
		player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 9
	end
end)

Edit: it’s done.

1 Like

Oh. You cannot get ‘LocalPlayer’ in a ServerScript, I believe.
Try using a PlayerAdded event! It’s like, you can’t access LocalPlayer in a ServerScript, and can’t access PlayerAdded in a server script.

If you have declared the variable clicks why don’t you use that instead?

Also, try using WaitForChild() so it will not get error as the assigned value is not loaded yet.

Edit: Just found out this is a Server Script

Its because you declared a variable clicks on the line 7 of the code. Removing it will fix it.

I did that
local event = game.ReplicatedStorage.ClickEvent

local button = script.Parent

local event = game.ReplicatedStorage.ClickEvent

local MarketPlaceService = game:GetService(“MarketplaceService”)

local GamePassID = 14905680

local player = game:GetService(“Players”).LocalPlayer

local userInput = game:GetService(‘UserInputService’)

event.OnServerEvent:Connect(function()

player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassID) then

player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 9

end

end)

image

You did it again…

Don’t USE LocalPlayer instance if the code is inside of a Script

How would I get the player then?

Like I stated above
Use game.Players.PlayerAdded

Crictoli fixed it for you.

1 Like

Theres a BUILT IN parameter in the OnServerEvent so it identifies who fired the RemoteEvent or somthing that camefrom the client.

RemoteEvent.OnServerEvent:Connect(function(PlayerWhoFired)
   print(PlayerWhoFired) --Prints the name of the player who fired the event from the client side
end)

I did that and it prints it, but it does not count it on leaderstats. Its still nil. image

What is your current script?
29chars

local script in gui
local button = script.Parent
local event = game.ReplicatedStorage.ClickEvent
local MarketPlaceService = game:GetService(“MarketplaceService”)
local GamePassID = 14905680
local player = game:GetService(“Players”).LocalPlayer
local clicks = player.leaderstats.Clicks
local userInput = game:GetService(‘UserInputService’)

button.MouseButton1Click:Connect(function()
event:FireServer()

end)

local function update_clicks()
player.PlayerGui.CLICK.Clicks.Text = "Clicks: "… tostring(clicks.Value)
end

clicks.Changed:Connect(function(update_clicks)
player.PlayerGui.CLICK.Clicks.Text = "Clicks: "… tostring(clicks.Value)
end)

userInput.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.Space then
event:FireServer()
player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

			if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassID) then
				player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 9
			end
		end
	end
end

end)

server script

local event = game.ReplicatedStorage.ClickEvent

local button = script.Parent

local event = game.ReplicatedStorage.ClickEvent

local MarketPlaceService = game:GetService(“MarketplaceService”)

local GamePassID = 14905680

local player = game:GetService(“Players”).LocalPlayer

local userInput = game:GetService(‘UserInputService’)

event.OnServerEvent:Connect(function(PlayerWhoFired)

print(PlayerWhoFired)

player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 1

if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamePassID) then

player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value + 9

end

end)

Your making the same mistake on the server

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

This player, will return the player on the client, but when this is done on the server, There is no local player
The server is responsible for the whole game and you cannot define one player this way

Additionally when you fire a remote event, the player is automatically passed as the first parameter.
ex.

local player = game:GetService("Players").LocalPlayer
RemoteEvent.OnServerEvent:Connect(function()
 player.leaderstats.Clicks.Value += 1
end)

This would be incorrect, since Local player doesn’t exist on the server, it is nil, which is why you get the error, Attempt to index nil (player) with leaderstats.

 RemoteEvent.OnServerEvent:Connect(function(player)
 player.leaderstats.Clicks.Value += 1
end)

This is the right way. Since player is passed as the first parameter, we can get the player who fired the event / who clicked. And do the function from there.

Would I use the service Players?