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

How Do I Fix Attempted to index nil with UserId?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
Despayr 505 Moderation Voter
3 years ago

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)

0
thank you very much! sonichfan 62 — 3y
0
Late response but im now a pro and looking back at my code i look very dumb sonichfan 62 — 2y
Ad

Answer this question