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

Attempt to index nil with humanoid? Cannot get humanoid from a character

Asked by 4 years ago

Heres the script with the problem (It's a script in ServerScriptService)

--// Also I want you to know that I'm very new to the marketplace service, and I only read one article.
--// Services
local market = game:GetService("MarketplaceService")

--// Variables
local speed_ID = 5659840164

local playerOwnsAsset = market.PlayerOwnsAsset

--// 2x gamepass

game.Players.PlayerAdded:Connect(function(player)
    local success, PlayerOwns2x = pcall(playerOwnsAsset, market, speed_ID, player)

    if PlayerOwns2x then
        print(player.Name.. " Owns 2x speed")
        local char = player.Character 
        local hum = char.Humanoid

        if hum ~= nil then
            print(player.Name.. " 2x speed enabled.")
            hum.WalkSpeed = hum.WalkSpeed * 2
        end
    elseif not PlayerOwns2x then
        print(player.Name.. " Doesn't own 2x speed")
    end
end)

I think getting the character is whats causing the problem. Can someone help me with this?

1 answer

Log in to vote
1
Answered by
megukoo 877 Moderation Voter
4 years ago

The character does not immediately exist after a player has joined the game. You may need to put the code that touches the character in an event called Player.CharacterAdded.

player.CharacterAdded:Connect(function(character)
    local humanoid = character.Humanoid
    -- this will run every time the player spawns, including respawns!
end)
0
Oh okay, I'll test it. Spookdoggz 42 — 4y
Ad

Answer this question