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

What can I do to stop an error of 'player'(a nill value)"?

Im not sure why im getting this error below in my extra cash script.

"Workspace.$150 CASH!.Head.Money Giver:12: attempt to index local 'player' (a nil value)"

the error is for line 12

local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) 

Its the script below.

local ting = 0 

function onTouched(hit)

	if ting == 0 then 
	ting = 1 
	local humanoid = hit.Parent:findFirstChild("Humanoid") 

	if ("Humanoid") ~= nil then 

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) 

		if cashmoney ~= nil then 
			 	
			cashmoney.Value  = cashmoney.Value +150
			wait(.1)
			script.Parent.Parent.Name = "1 min"
			wait(30)
			script.Parent.Parent.Name = "30 sec"
			wait(30)
			script.Parent.Parent.Name = "$150 CASH!" 	
		end

	end

	ting = 0 
	end

end

script.Parent.Touched:connect(onTouched)

1 comment

0

Im not sure why it spaced down like that, its not like that in the script.

1 answer

✓ Accepted answer

The error says that player is nil. Why might that be?

You should look at where you defined player:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

The Wiki article for GetPlayerFromCharacter reads

Returns the Player associated with the given character, or nil if the [parameter] is not controlled by [any] player.

That means we get nil if hit.Parent isn't actually any player's Character (or for that matter a Tool or Hat in a real character)

This would happen if some random part touched your button.


The solution is to only advance when you're sure player is not nil:

			if player then
				local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) 
				if cashmoney ~= nil then 
					cashmoney.Value  = cashmoney.Value +150
			-- etc

4 comments

0

im very new to scripting, im trying to do as much as I can with out getting exact answers but in this case im not exactly sure. Do I replace line 12 with this line? Thnaks so much for your help.

0

Wait I think I get it. The script I have is asking to get player from hit.Parent and the what you added is to state that if hit.Parent is player then... ext am I close?

0

Just like you check that cashmoney ~= nil, you need to check that player isn't nil. In the snippet you posted, that means inserting an if player ~= nil before line 12 and a matching end after line 23.

0

ok thanks so much

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