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

Whats wrong with my 2x points gamepass? Script gives an error

Asked by 3 years ago

So i have mede a portal, when you touch it you got 1 stage completed and it need to give 60 points if player haves the gamepass, but i got wrong on something. The output says ServerScriptService.Script:13: attempt to index nil with 'UserId' There is my script:

local db = false --Adding a debounce
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 24389862
local player = game.Players.LocalPlayer

    local portal = workspace.FirstStage.FrostPartal.PortalPart --Defining the portal

    portal.Touched:Connect(function(hit) --Touched function
            if hit.Parent:FindFirstChild("Humanoid") and not db then --Checking if a player touched it and that debounce is false
                    db = true --Making debounce true
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Getting the player that touched it
        plr.leaderstats.Stages.Value = plr.leaderstats.Stages.Value + 1 --Adding value
        if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 60
            print("nice!")
    else
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 30
                    wait(10) --The cooldown for the debounce, you can make this as much as you want
                    db = false --Making debounce false
        end
    end
        end)

0
Is this code in a local script or a server script? l_diue 30 — 3y
0
Nvm i fixed that SashaPro336 47 — 2y

1 answer

Log in to vote
0
Answered by
l_diue 30
3 years ago

You used the variable "player", If this is a server script which I'm assuming it is you cant use "game.Players.LocalPlayer." In the code you tried to check and see if the player owned the gamepass, but since the SERVER SCRIPT cant use that. Therefore the variable "player" returns as nil, so you want to use the "plr" variable you made.

local db = false --Adding a debounce
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 24389862
local player = game.Players.LocalPlayer

    local portal = workspace.FirstStage.FrostPartal.PortalPart --Defining the portal

    portal.Touched:Connect(function(hit) --Touched function
            if hit.Parent:FindFirstChild("Humanoid") and not db then --Checking if a player touched it and that debounce is false
                    db = true --Making debounce true
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Getting the player that touched it
        plr.leaderstats.Stages.Value = plr.leaderstats.Stages.Value + 1 --Adding value
        if MarketPlaceService:UserOwnsGamePassAsync(plr.UserId, GamepassID) then
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 60
            print("nice!")
    else
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 30
                    wait(10) --The cooldown for the debounce, you can make this as much as you want
                    db = false --Making debounce false
        end
    end
        end)

Ad

Answer this question