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

Player is a nil Value?

I'm making a game that has a cash IntValue in it and it says that Players.CreeperAssassin005.Backpack.RustyShovel.Script:13: attempt to index local 'player' (a nil value). Any help?

script.Parent.blade.Touched:connect(function(hit)

if script.Parent.CanDamage.Value == true then

script.Parent.CanDamage.Value = false

hit.Parent.Humanoid:TakeDamage(20)

if hit.Parent.Humanoid.Health == 0 then
local  humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
	  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
      local money = player.leaderstats.Cash
      money.Value = money.Value + 999999
end
end
end
end)

3 answers

try this

script.Parent.blade.Touched:connect(function(hit)

if script.Parent.CanDamage.Value == true then

script.Parent.CanDamage.Value = false

hit.Parent.Humanoid:TakeDamage(20)

if hit.Parent.Humanoid.Health == 0 then
local  humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
if player ~= nil then
      local player = game.Players:GetPlayerFromCharacter(hit.Parent)
      local money = player.leaderstats.Cash
      money.Value = money.Value + 999999
end
end
end
end
end)

4 comments

0

It doesn't have any error messages but it doesn't give my the money...

0

Using Print I figured out that the code stops at if player ~= nil then, so this code doesn't work in this state.

0

Line 12 has to go after line 13...

0

^already tried that

script.Parent.blade.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
		if script.Parent.CanDamage.Value == true then
	   		  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
 			  local money = player:WaitForChild(“leaderstats”).Cash
				script.Parent.CanDamage.Value = false
				hit.Parent.Humanoid:TakeDamage(20)
    				money.Value = money.Value + 999999
		end
	end
end)

4 comments

0

Doesn't even do damage now :/

0

try it now my boi

0

nope :/

0

hm

script.Parent.blade.Touched:connect(function(hit)
	if script.Parent.CanDamage.Value == true then
		script.Parent.CanDamage.Value = false
		if hit.Parent:findFirstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(20) end
		if hit.Parent.Humanoid.Health == 0 then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player then
				local money = player.leaderstats.Cash
				money.Value = money.Value + 999999
			end
		end
	end
end)

And if that doesn't work, there is probably something wrong with your tool. Try debugging it before saying it doesn't work. Use print() and warn().

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