Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

I need help identifying the problem with this script. (?)

Asked by 6 days ago

The script is for a button when touched spawns a vehicle only if you have bought the gamepass. But it only works for me not others who have bought the gamepass.

Where its located in workspace: https://i.gyazo.com/fe66c19cfa745e6c6de2ef72756152d1.png

Script name is "Activate" in that pic.

Script:

button = script.Parent

regen = button.Parent.Regen


local debounce = false



function onTouch(hit)

    local h = hit.Parent:FindFirstChild("Humanoid")
    local user = game.Players:GetPlayerFromCharacter(hit.Parent)
    local stats = user:findFirstChild("leaderstats")
    local cash = stats:findFirstChild("Gold")
    local gamepassId = script.Parent.GamepassId
    local marketplaceservice = game:GetService("MarketplaceService")


    if h ~= nil and debounce == false and user and marketplaceservice:PlayerOwnsAsset(user, gamepassId.Value) then

        debounce = true

        button.BrickColor = BrickColor.Black()

        regen.Value = 1

        wait(1)

        regen.Value = 0

        wait(30)

        button.BrickColor = BrickColor.new(104)

        debounce = false

    end

end

button.Touched:connect(onTouch)

2 answers

Log in to vote
1
Answered by
incapaxian 1315 Badge of Merit Moderation Voter
6 days ago
Edited 6 days ago

Not sure why it only works for you, but I found another issue.

An update was made to game passes in April of 2018, which made them no longer assets. You should be using the :UserOwnsGamePassAsync() function instead. Note that this function takes the UserId as an argument rather than the player instance itself.

-- # Services
local MarketplaceService = game:GetService("MarketplaceService") -- # You do not need to declare this variable each time the part is touched!
local Players = game:GetService("Players") -- # Stay consistent! Why make a variable for MarketplaceService and call :GetService() on MarketplaceService but not for Players?

local button = script.Parent -- # always use local variables
local regen = button.Parent.Regen
local gamePassId = button.GamepassId
local debounce = false

local function onTouch(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent)

    if player and not debounce then -- # == false not needed
        local success, ownsPass = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, player.UserId, gamePassId.Value)
        -- # always wrap web requests in pcall (protected call)

        if success and ownsPass then -- # no errors occurred, and they own the pass
            debounce = true
            button.BrickColor = BrickColor.Black()
            regen.Value = 1
            wait(1)
            regen.Value = 0
            wait(30)
            button.BrickColor = BrickColor.new(104)
            debounce = false
        end
    end
end

button.Touched:Connect(onTouch) -- # :connect is deprecated, use :Connect. 

Things I used, but never explained

pcall - Short for "protected call", this function calls the function you pass in protected mode, so any errors will not propagate. Note that pcall only catches runtime errors, so if you have a syntax error, pcall will not catch it since syntax errors are what prevent your code from compiling in the first place. The first return value of pcall will always be a boolean. If pcall returns true, that is because the function you passed did not error, false otherwise. If the function you pass has return values the return values after the first will be the return values of the function. pcall second return value will be a string which is the error message if pcall returned false (there was an error).

Ad
Log in to vote
0
Answered by 6 days ago

New gamepasses do not have an Asset ID, only a Gamepass ID. As such, you cannot use MarketplaceService:PlayerOwnsAsset() because there is no Asset ID for a gamepass. Instead, you should be using MarketplaceService:UserOwnsGamePassAsync().

button = script.Parent
regen = button.Parent.Regen
local debounce = false

function onTouch(hit)

    local h = hit.Parent:FindFirstChild("Humanoid")
    local user
    local stats
    local cash
    if h then
        local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        local stats = user:findFirstChild("leaderstats")
        local cash = stats:findFirstChild("Gold")
    end
    local gamepassId = script.Parent.GamepassId
    local marketplaceservice = game:GetService("MarketplaceService")

    if h and not debounce and marketplaceservice:PlayerOwnsAsset(user.UserId, gamepassId.Value) then

        debounce = true

        button.BrickColor = BrickColor.Black()

        regen.Value = 1

        wait(1)

        regen.Value = 0

        wait(30)

        button.BrickColor = BrickColor.new(104)

        debounce = false

    end

end

button.Touched:Connect(onTouch)

I also fixed some of the other errors in the code:

  • Checked if the humanoid exists to prevent the script from throwing an "[x] is not a valid member of [y]" error

  • If h exists, user must also exist, so I removed the user check

  • Changed if h ~= nil and debounce ~= false to if h and not debounce

  • Changed button.Touched:connect(onTouch) to button.Touched:Connect(onTouch) because :connect() is deprecated

Answer this question