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

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

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

✓ Accepted answer

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)

1 comment

0

Oh okay, I'll test it.

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