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

[ANSWERED] attempt to index local 'Player' (a nil value) Error?

I get this error:

15:13:52.062 - Workspace.Detect.Main:6: attempt to index local 'Player' (a nil value) 15:13:52.063 - Stack Begin

15:13:52.063 - Script 'Workspace.Detect.Main', Line 6 15:13:52.064 - Stack End

15:13:52.064 - Disconnected event because of exception

on this script:

local Enabled = true

function onClick(Brick)
	if Enabled == true then
		local Player = Brick.Parent:findFirstChild("Humanoid")
		local Character = game:GetService('Players'):GetPlayerFromCharacter(Player.Parent)
		Enabled = false
		print("Stage 1")
		if Player ~= nil then
			print("Stage 2")
			if Player.Parent.Car ~= nil then
				print("Stage 3")
				if Character.Money.Value >= 50 then
					print("Stage 4")
					Player.Car.Fuel = player.Car.Fuel + 50
					Character.Money.Value = Character.Money.Value - 50
					wait(5)
					Enabled = true
				end
			end
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

I've gone over it multiple times and I can't find an error. Can anyone please help?

3 answers

✓ Accepted answer

Here's a fix:

local Enabled = true

function onClick(Player)
    if Enabled == true then
        Enabled = false
        print("Stage 1")
        if Player ~= nil then
            print("Stage 2")
            if Player.Parent.Car ~= nil then
                print("Stage 3")
                if Character.Money.Value >= 50 then
                    print("Stage 4")
                    Player.Car.Fuel = player.Car.Fuel + 50
                    Character.Money.Value = Character.Money.Value - 50
                    wait(5)
                    Enabled = true
                end
            end
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClick)

When a function is connected to the MouseClick event, the player who clicked the part will be passed as the first argument, not any of the player's parts.

local Character = Brick.Parent:FindFirstChild("Humanoid") local Player = game.Players:GetPlayerFromCharacter(Character)

Your Character and Player variables were swapped and when you attempted to get the player(it's variable was Character) then you were using the Parent of the character for the parameters of the GetPlayerFromCharacter event.

Hope I Helped

+1

2 comments

0

I only get Stage 1 printed. Stage 2 is not printed.

0

Well that's because you didn't fix the other problems that rooted from that first one ._.

The error is Player (a nil value)

function Buy(Player)
	local RS = game:GetService("ReplicatedStorage")
	local item = RS:WaitForChild("Ball")
	local Price = 50
	local stats = Player.stats
	if (stats.Cash.Value >= Price) then
		item:Clone().Parent = Player.Backpack
		item:Clone().Parent = Player.StarterGear
		stats.Cash.Value = stats.Cash.Value - Price
	end
end

script.Parent.MouseButton1Click:connect(Buy)

1 comment

0

If this is in a Gui, then u need to verifie the player. if script is in player u can us - script.Parent - or if it in Humanoid - script.Parent.Parent - . I tryed it my self with an oke buton, but if i clicked the oke button i did get the same error. nil means nothing. so i advise to put the script in the player him self and use - Player = script.Parent - else u have to say some how Player = ....

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