Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Argument 1 missing or nil error on Line 4?

Asked by
zomspi 541 Moderation Voter
4 years ago

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

Log in to vote
0
Answered by
Norbunny 555 Moderation Voter
4 years ago
Edited 4 years ago

"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

0
Thanks! How would I use a remote event to pass the players name? zomspi 541 — 4y
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! Norbunny 555 — 4y
0
Also it puts this error "bad argument #2 (string expected, got nil)" zomspi 541 — 4y
0
on line 4, thanks! zomspi 541 — 4y
View all comments (4 more)
0
Are you sure that you wrote any text into the field? If there's no text, it will return nil. Norbunny 555 — 4y
0
I see that there is something called placeholder text, is that what I should be using? zomspi 541 — 4y
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! Norbunny 555 — 4y
0
The thing is, I write my username, but it still returns nil? I used a remote event zomspi 541 — 4y
Ad
Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago
Edited 4 years ago

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)

0
The potatoes prints the number I put into the potatoes but the name just prints nil zomspi 541 — 4y

Answer this question