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

invalid argument #3 (string expected, got nil) How to fix?

Hello there! I am having massive trouble with my ban gui. I'm testing the ban gui and I type a reason (string). I press the ban button which fires a remote event. This error pops up in the output: invalid argument #3 (string expected, got nil). I have no idea how to fix it.

Remote event code:

game.ReplicatedStorage.banPlayer.OnServerEvent:Connect(function(target, reason)
	target.Banned.Value = "Yes"
	target.banReason.Value = reason
	target:Kick(reason)
end)

The error is coming from line 3.

Ban button code:

local playerInput = script.Parent.Parent.PlayerInput
local ReasonInput = script.Parent.Parent.ReasonInput
local targetedPlayer = game.Players:FindFirstChild(playerInput.Text)
local toString = tostring(ReasonInput.Text)

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.banPlayer:FireServer(targetedPlayer, toString)
end)

Any ideas? I would greatly appreciate some debugging with my code. :D

1 answer

OnServerEvent automatically have the first argument as the player who fired the remote event(same with remote function).

Also, you aren't considering if the "targetedPlayer" variable in the ban code is nil, meaning it wasn't able to find a player with that name and this can be fixed by making sure the "targetedPlayer" variable is valid.

Lastly, converting "ReasonInput.Text" to string is pointless since its probably from a text box which is already a string and changing values in the target is also pointless as the player is getting kicked any way, making the values useless.

Ban button code could look something like this:

local ReasonInput = script.Parent.Parent.ReasonInput.Text
local targetedPlayer = game.Players:FindFirstChild(script.Parent.Parent.PlayerInput.Text)


script.Parent.MouseButton1Click:Connect(function()
if targetedPlayer then -- checks if its a valid player

game.ReplicatedStorage.banPlayer:FireServer(targetedPlayer, ReasonInput)

end
end)

Remote event code could look like this:

game.ReplicatedStorage.banPlayer.OnServerEvent:Connect(function(player, target, reason)
    target:Kick(reason)
end)

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