Issue with script that I don't know how to fix

Hi, so I’m having trouble with a script. So when you buy the game pass it gives you a gun from ServerStorage.

local Player =  game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassId = 17171191


if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) then
print("player does not own pass")
	local tool = game.ServerStorage["Historic oof gun"]
	local klone = tool:clone()
	script.Parent.ClickDetector.MouseClick:connect (function(plr)
		if klone.Parent ~= plr.StarterPack then
			klone.Parent = plr.StarterPack
		end
	end)
end

But every time i run it on an account that has the game pass I get this message error message in output
image
Does anyone know what I did wrong?
All help is appreciated (:

1 Like

Do game:GetService("Players").LocalPlayer on line 1 when you define Player

1 Like

I did that but I still get the error message.

LocalPlayer does not work in Server scripts, use the PlayerAdded event of the Player’s service to get a player to get the UserId from, aka, when a player joins, compare if they have the gamepass and if they do ,do the stuff

local tool = game.ServerStorage[“Historic oof gun”]

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local GamepassId = 17171191
Players.PlayerAdded:Connect(function(plr)
	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, GamepassId) then
		print("player owns pass")
		local klone = tool:clone()
		script.Parent.ClickDetector.MouseClick:connect(function()
			if klone.Parent ~= plr.StarterGear then
				klone.Parent = plr.StarterGear
			end
		end)
	end
end)

You may need to change the script.Parent to something else I believe, although not sure if you even need it since you can just do this

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")

local tool = game.ServerStorage["Historic oof gun"]

local GamepassId = 17171191

Players.PlayerAdded:Connect(function(plr)
	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, GamepassId) then
		print("player owns pass")
		tool:Clone().Parent = plr.Backpack
		tool:Clone().Parent = plr.StarterGear
	end
end)
4 Likes

Is this in a Script or a LocalScript? .LocalPlayer only works in a LocalScript.

1 Like

its a script inside of srever script service

Try doing what @EmbatTheHybrid suggested and use the PlayerAdded event.

1 Like

I get the same error and it says Unknown Global ‘player’

Mind screenshotting your code to see what you mean?

Accidentally forgot to replace Player with plr, try now. Unles you’re using yuor own own come in which case show us

That worked now! Thanks For all of your help!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like