Argument 1 missing or nil

Hey there!
So currently I’m making a drive for cash script, but I have run into some problems.
I am getting a Argument 1 missing or nil error and I don’t know why.
The script is placed inside the collider and finds the driver or player, whatever you want to call it.
The line player.leaderstats.cash.value works.

local player
local Distance = script.Distance


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		player = game.Players:GetPlayerFromCharacter(hit.Parent)
	end
end)


while true do
	wait(1)
	local DistanceCash = require(game.ServerScriptService.DistanceCash)
	local PlayerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player)
--	local PlayerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player)
--	print(PlayerMoney.Value)
	

	Distance.Value = Distance.Value + script.Parent.Velocity.Magnitude
	if Distance.Value > 100 then
		
		--player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + DistanceCash.C46
		
		PlayerMoney.Value = PlayerMoney.Value + 100
		player.leaderstats.Distance.Value = player.leaderstats.Distance.Value + 0.1
		
		
		Distance.Value = 0
		
	
	end

end

When I type in the script print player, it prints my name “SuperiusSolutions”, so it know that I am the driver.
I tried to do a findfirstchild(player) in the hopes it would find the correct name in my folder, but no luck.

What am I doing wrong?

At what line does it error out? (it should say in the error)

1 Like

the part where you get the player’s money the script does this

  1. while true
  2. yield for 1 second
  3. look in some folder for a player object
  4. player is nil
  5. error
  6. loop breaks
  7. script pauses

your player is nil because nothing touched the part in the connection above the while loop

make sure to check if the player exists before running anything in the while loop

also you are using FindFirstChild with a player object FindFirstChild takes in a string not an object
change player to player.Name

3 Likes

When I click the error the line “local PlayerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player)” gets highlighted.

2 Likes

it is most likely what bIorbee said then

1 Like

Your best case is to check if there even is a player (just adding on what @blorbee said)

while true do
	wait(1)
	if player then
	--code here
	end
end
3 Likes

@SuperiusSolutions please do not mark my answer as solution, @bIorbee was the first one to mention it, mark his answer instead

1 Like

Thank you all so much for the fast answers! Exactly what I needed! :slight_smile:

local player
local Distance = script.Distance


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		player = game.Players:GetPlayerFromCharacter(hit.Parent)
	end
end)


while true do
	wait(1)
	if player then
	local DistanceCash = require(game.ServerScriptService.DistanceCash)
	local PlayerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
	--	local PlayerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player)
	--	print(PlayerMoney.Value)


	Distance.Value = Distance.Value + script.Parent.Velocity.Magnitude
	if Distance.Value > 100 then

		--player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + DistanceCash.C46

		PlayerMoney.Value = PlayerMoney.Value + 100
		player.leaderstats.Distance.Value = player.leaderstats.Distance.Value + 0.1


		Distance.Value = 0


	end
end
end

Some errors corrected and new knowledge accuired! :grinning:

1 Like

I needed this line to, since you are able to sell the car, and it threw some errors when it couldn’t find the player anymore, so both was needed! :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.