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

How Do I Fix Attempted to index nil with UserId?

local GamePassID = 18211591

game.Players.PlayerAdded:Connect(function()

game:GetService("MarketplaceService")

if MarketPlace:UserOwnsGamePassAsync(player.UserId, GamePassID) then
	game.Workspace.WaitForChild(player.Name):WaitForChild("Humanoid").WalkSpeed = 25
end

end)

How Do I Fix Attempted to index nil with UserId

BTW the script is in serverscriptstorage

1 answer

✓ Accepted answer

It is erroring because you haven't set a value to recieve the parameter, so the script has no idea what you mean when you are referencing "player".

Next up, you haven't set a variable for marketplace service on line 1, so that will error too.

Finally, I can see that you are changing the characters walkspeed if they have the gamepass. You should not put this code in a playeradded event, but a characteradded event instead. The playeradded event only fires when a player joins, but the characteradded event fires whenever the players character spawns.

There is also no need to keep on defining MarketPlaceService, when you can just define it once at the top of the script.

Here is the fixed code

local MarketPlace = game:GetService("MarketplaceService")
local GamePassID = 18211591

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local hasPass = MarketPlace:UserOwnsGamePassAsync(player.UserId, GamePassID)
		if hasPass then
			local human = character:FindFirstChildOfClass("Humanoid")
			if human ~= nil then
				human.WalkSpeed = 25
			end
		end
	end)
end)

2 comments

0

thank you very much!

0

Late response but im now a pro and looking back at my code i look very dumb

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