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)
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)
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.
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)
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â)
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
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.
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.