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

Argument 1 missing or nil error on Line 4?

It says there is an error on line 4 (Argument 1 missing or nil), I don't know what this means, thanks!

script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Name.Text
	local potatoes = script.Parent.Potatoes.Text
	if game.Players:FindFirstChild(name) then
		game.Players:FindFirstChild(name).leaderstats.Potatoes.Value = game.Players:FindFirstChild(name).leaderstats.Potatoes.Value + potatoes(tonumber())
	end
	
end)

2 answers

"Argument missing or nil" means that the argument hasn't been passed or doesn't exist.

In example, if the player you are trying to find doesn't exist, it is said to be "nil".

You should also create a variable for your player, it will be easier to reference it that way!

local players = game:GetService("Players")

local player = players[name]

-- And use
if(player) then
	-- if the player is found, run your code
end

Warning! You are trying to change a user's stats from a LocalScript, meaning that it will only change for you, or the user who's using it! In order to makes changes that are seen by everyone, you need to learn to use "RemoteEvents"!

There is good documentation and examples in Roblox's documentation!

Documentation on:

RemoteEvents

8 comments

0

Thanks! How would I use a remote event to pass the players name?

0

A RemoteEvent lets you pass various arguments, you would just pass it with any other arguments. Once more, I suggest checking out Roblox's documentation on RemoteEvents, it will give you better understanding on it!

0

Also it puts this error "bad argument #2 (string expected, got nil)"

0

on line 4, thanks!

0

Are you sure that you wrote any text into the field? If there's no text, it will return nil.

0

I see that there is something called placeholder text, is that what I should be using?

0

Nope, placeholder text is the text shown when you don't write anything into the field! .Text will return the text you've input. But if you haven't written anything, it returns nil!

0

The thing is, I write my username, but it still returns nil? I used a remote event

Remote event server script

game.ReplicatedStorage.PotatoesCommand.OnServerEvent:Connect(function(player, name, potatoes)
	print(name)
	print(potatoes)
	print("yes")
	local players = game:GetService("Players")
	local plr = players[name]
	
	if (plr) then
		plr.leaderstats.Potatoes.Value = plr.leaderstats.Potatoes.Value + tonumber(potatoes) 
	end
end)

Local Script

script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Name.Text
	local potatoes = script.Parent.Potatoes.Text
	if name ~= "" and potatoes ~= "" then
		game.ReplicatedStorage.PotatoesCommand:FireServer(name, potatoes)
	end
	
end)

1 comment

0

The potatoes prints the number I put into the potatoes but the name just prints nil

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