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 field 'Character' (a nil value)?

My drink script gives me the error on the title when I use it out of Roblox Studio. Please help me fix it. Here is the script: It says the error is on line 3

local Plr = game.Players.LocalPlayer

local Humanoid = Plr.Character:WaitForChild("Humanoid")

local Tool = script.Parent;


enabled = true




function onActivated()
	if not enabled  then
		return
	end

	enabled = false
	Tool.GripForward = Vector3.new(-0, -0.836, -0.549)
	Tool.GripPos = Vector3.new(1.4, -0.4, 0.3)
	Tool.GripRight = Vector3.new(0.996, -0.0509, 0.0776)
	Tool.GripUp = Vector3.new(0.0928, 0.547, -0.832)


	Tool.Handle.DrinkSound:Play()
	Humanoid.WalkSpeed = Humanoid.WalkSpeed +.5
	game.Players.LocalPlayer.leaderstats.Speed.Value = game.Players.LocalPlayer.leaderstats.Speed.Value +3

	wait(3)
	
	local h = Tool.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		if (Tool.Parent:FindFirstChild("SpeedEffect") == nil) then
			local s = script.Parent.SpeedEffect:Clone()
			s.Disabled = false
			s.Parent = Tool.Parent			
		end
	end


	Tool.GripForward = Vector3.new(-.976,0,-0.217)
	Tool.GripPos = Vector3.new(0.2,-.1,0)
	Tool.GripRight = Vector3.new(.217,0,-.976)
	Tool.GripUp = Vector3.new(0,1,0)


	wait(1)
	enabled = true

end



script.Parent.Activated:connect(onActivated)


2 answers

✓ Accepted answer

Character of a player isn't supposed to be managed as a child of player, but a property.

Between line 1 and 3 just add

local char = plr.Character or plr.CharacterAdded:wait()

it always seem to work for me :)

1 comment

0

good answer.

For some reason, the Character property bugs many scripts.

Instead of...

local Humanoid = Plr.Character:WaitForChild("Humanoid")

Try...

local chr = Plr.Character
if chr == nil then
  repeat
   -- do nothing.
  until chr ~= nil or chr == true
  -- This repeat loop will keep running until the character is valid. It will then continue the script.

end
local Humanoid = Plr.Character:FindFirstChild("Humanoid") -- The Humanoid is packed with the Character model already.

I haven't tested it. It's Midnight here in the Philippines so chances are it might fail.

Hoped I helped if it worked otherwise.

4 comments

0

I might be availanle tomorrow after school. Anyways Good night from the Philippines.

0

You wouldn't want to 'do nothing' inside of the repeat. chr is only being set to character once, so if it's nil initially, that's an infinite loop, so you would want do continuously set chr = Plr.Character until it's not nil.

0

Why would you check if chr equals true

0

Can I get code like the full script with this in it. What I did with this is not working. Same error.

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