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

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

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)

2 comments

0

Is this code in a local script or a server script?

0

Nvm i fixed that

1 answer

✓ Accepted answer

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)

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