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

GamePass Tool is not given when part touched ?

So i created a game pass and i wanted to link it with a part that would give you the tool if you have a game pass. But the script won't work at line 21.

Here's my code

local debounce = true
local weapon = game.ReplicatedStorage.Shuriken
local player = game:GetChildren("Players").localPlayer
local GamePassService = game:GetService('GamePassService')
local player1 = game.Players.LocalPlayer
local part = script.Parent
local cooldown = 3
id = 12345 --gamepass id

local sound = Instance.new("Sound")
sound.SoundId = "http://www.roblox.com/asset?id=3302699870"
sound.Parent = script.Parent


local function GiveTool(plr)
	local hit = plr.Parent
	local humanoid = hit:FindFirstChildWhichIsA("Humanoid")
	if humanoid and debounce == true then
		print("pass1")
		local bag = game.Players:GetPlayerFromCharacter(hit).Backpack
		if GamePassService:PlayerHasPass(player1.userid, id) then -- there is an error
			print("pass2")
			local weaponCopy = weapon:Clone()
			weaponCopy.Parent = bag
			sound:Play()
			debounce = false
			wait(cooldown)
			debounce = true
		end
	end
end


part.Touched:Connect(GiveTool)

Can you explain why it doesn't work ?

ERROR : Workspace.Model.Give.ScriptGiver:21: attempt to index upvalue 'player1' (a nil value)

1 comment

0

Is this a script or localscript?

3 answers

✓ Accepted answer

Hello, the UserID property of the player is not "userid" but instead "UserId" So change line 21 to:

if GamePassService:PlayerHasPass(player1.UserId, id) then

Also good measures for print debugging.

2 comments

0

ok thnx

0

Lizard press the accept answer below if it solved your question.

player1 is defined to be game.Players.LocalPlayer, but on a server script there is no local player. Make sure this is in a LocalScript. I know this has been answered already, but "attempting to index variable (a nil value)" almost always means variable was not defined properly.

1 comment

1

Your right.. good idea

I've found the answer, by the way thnx for replying :).

local debounce = true
local weapon = game.ReplicatedStorage.Shuriken
local player = game:GetChildren("Players").localPlayer
local cooldown = 3


local GamePassService = game:GetService('MarketplaceService')
local PlayersService = game:GetService('Players')
local part = script.Parent
local GamePassIdObject = 7896377

local function GiveTool(plr)
	local hit = plr.Parent
	local humanoid = hit:FindFirstChildWhichIsA("Humanoid")
	if humanoid and debounce == true then
		local bag = game.Players:GetPlayerFromCharacter(hit).Backpack
		local player = PlayersService:GetPlayerFromCharacter(plr.Parent)
		if GamePassService:UserOwnsGamePassAsync(player.userId, GamePassIdObject) and debounce == true then
			local weaponCopy = weapon:Clone()
			weaponCopy.Parent = bag
			debounce = false
			wait(cooldown)
			debounce = true
		else
			local player = PlayersService:GetPlayerFromCharacter(plr.Parent)
			GamePassService:PromptGamePassPurchase(player, GamePassIdObject)
		end
	end
end


part.Touched:Connect(GiveTool)

I found how to fix it by finding a free model of a VIP door that had similar functions of what i wanted for my tool giver to do.

I hope this has helped someone else in the future ????

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