Why am I getting this warning?

Hello all,

Right to the point, I’m getting this error:

10:53:43.668 :arrow_forward: Workspace.Base.Buttons.Claim this castle!.ClaimButtonScript:19: attempt to index nil with ‘UserId’ (x10)

In this script:

function steppedOn(part, player)
	
	local hasPass = false

	-- check if the player has the game pass
	local success, message = pcall(function() 
		hasPass = Market:UserOwnsGamePassAsync(player.UserId, gamePassId1)
	end)

	-- stop function if something goes wrong
	if not success then
		warn(message)
		return
	end

	-- if the player owns the game pass
	if hasPass then
		-- give plyr the trail
		extrapartsAppear(player)
	end
	
	extrapartsAppear()
	
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	
	if player and player:FindFirstChild('ownsBase').Value == false then
		
		script.Parent.Parent.Parent:FindFirstChild('Owner').Value = player
		player:FindFirstChild('ownsBase').Value = true
		player:FindFirstChild('Base').Value = script.Parent.Parent.Parent
		script.Parent:Destroy()
		
	end
	
end
button.Touched:Connect(steppedOn)

There’s more to the script, I’m only showing the part that needs work right now. The error is referring to line 5 with the player.UserId, gamePassid1.

Any help is appreciated. Thank you!

Player is not a parameter of the Touched event. You have to get the player from the part that touched the block.

function steppedOn(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if not player then return end
	local hasPass = false

	-- check if the player has the game pass
	local success, message = pcall(function() 
		hasPass = Market:UserOwnsGamePassAsync(player.UserId, gamePassId1)
	end)

	-- stop function if something goes wrong
	if not success then
		warn(message)
		return
	end

	-- if the player owns the game pass
	if hasPass then
		-- give plyr the trail
		extrapartsAppear(player)
	end
	
	extrapartsAppear()
	
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	
	if player and player:FindFirstChild('ownsBase').Value == false then
		
		script.Parent.Parent.Parent:FindFirstChild('Owner').Value = player
		player:FindFirstChild('ownsBase').Value = true
		player:FindFirstChild('Base').Value = script.Parent.Parent.Parent
		script.Parent:Destroy()
		
	end
	
end
button.Touched:Connect(steppedOn)
2 Likes

He has defined player by getting the character through the touched part’s Parent which is Character.

1 Like

All he needs to do is remove player parameter.

1 Like

It works! Thank you so much for your kind help!