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

How to make this Script get the local player?

I have a gamepass as follows:

local player = game:GetService("Players")
local id = Here is my gamepass id
local ownsGamepass2x = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id)


game.ReplicatedStorage.GiveCoins.OnServerEvent:Connect(function(X, player)
	
	if ownsGamepass2x then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 2
	else
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
	end
	
   
end)

So as you can see, if the player owns the game pass, then we want to give them 2 coins and if they don't, then we want to give them 1 coin.

On line 3, UserOwnsGamePassAsync take the player.UserId. But I can't get the player. The output is saying attempt to index nil with UserId.

I can't use game.Players.LocalPlayer because this is a script which check if the user owns the gamepass and not a local script.

How can I go about finding the player so I can get the player.UserId?

Thanks for any help

3 comments

0

well then make it a localscript.

0

The first variable in any remoteevent function to the server is the player. That means that X, is infact- the player. Try using X instead

0

@marinhren Thanks. It worked!

2 answers

✓ Accepted answer

Always the first argument of OnServerEvent will be the player, even without firing the player with :FireServer(). Here is how it should look like:

local player = game:GetService("Players")
local id = Here is my gamepass id
local ownsGamepass2x = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id)


game.ReplicatedStorage.GiveCoins.OnServerEvent:Connect(function(player)

    if ownsGamepass2x then

        player.leaderstats.Coins.Value += 2 -- use += operator

    else

        player.leaderstats.Coins.Value += 1 -- same here

    end

end)

Make sure on the local script, the :FireServer() function looks like remote:FireServer() without any args. Hope this helped!

You can just make a function when a player is joining the game:

game.Players.PlayerAdded:Connect(function(player)

SCRIPTHERE

end)

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