Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

GamePass Tool is not given when part touched ?

Asked by
Tixabel 19
5 years ago

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)

0
Is this a script or localscript? compUcomp 417 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago

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.

0
ok thnx Tixabel 19 — 5y
0
Lizard press the accept answer below if it solved your question. 123nabilben123 499 — 5y
Ad
Log in to vote
1
Answered by
compUcomp 417 Moderation Voter
5 years ago

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
Your right.. good idea 123nabilben123 499 — 5y
Log in to vote
1
Answered by
Tixabel 19
5 years ago

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 ????

Answer this question