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

Server Script Gives Nil Error When Trying To Find Player, How to Fix?

So I'm making a kick gui using functions

the local script works already but for some reason the server script isn't

local ReplicatedStorage = script.Parent.Parent
local RemoveEvent = ReplicatedStorage.kick
RemoveEvent.Name = "kick"

ReplicatedStorage.kick.OnServerEvent:connect(function(v1,v2)
	print('test')
	local kname = v1
	print(kname)
	local kreason = v2
	local kp = game.Players:FindFirstChild(kname)
	print(kp.Name)
	print('test2')
	kp:Kick(kreason)
end)

I keep getting this error

attempt to index local 'kp' (a nil value)

EDIT:

here is the localscript

local ReplicatedStorage = script.Parent.Parent.Parent.Parent.Parent.Functions
local RemoteEvent = ReplicatedStorage:WaitForChild("kick")
script.Parent.MouseButton1Click:Connect(function()
	local v1 = script.Parent.Parent.kickname.Text
	local v2 = script.Parent.Parent.kickreason.Text
	print(v1..' | '..v2)
	if game.Players:FindFirstChild(v1) then
	RemoteEvent:FireServer(v1,v2)
	print(v1..' dadd | dadd '..v2)
	end
end)

2 answers

✓ Accepted answer

The reason is simple. The first parameter you get from OnServerEvent is the actual player instance. So you don't need the code on line 10 since you already have the player. If you wanted you could say local kp = game.Players:FindFirstChild(v1.Name)

Also, it would be great if you could say on what line you get the error. It makes it easier to debug.

local ReplicatedStorage = script.Parent.Parent
local RemoveEvent = ReplicatedStorage.kick
RemoveEvent.Name = "kick"

ReplicatedStorage.kick.OnServerEvent:connect(function(Player,Reason)
    Player:Kick(tostring(Reason))
end)

(Why do you on line 2 set the variable as ReplicatedStorage.kick and then on line 3 rename it to kick again, when it's already named kick?)

1 comment

0

oof I forgot to delete line 3 lol

local ReplicatedStorage = script.Parent.Parent local RemoveEvent = ReplicatedStorage.kick RemoveEvent.Name = "kick"

ReplicatedStorage.kick.OnServerEvent:connect(function(player, v1,v2) -- SOLUTION: ADD PLAYER VALUE print('test') local kname = v1 print(kname) local kreason = v2 local kp = game.Players:FindFirstChild(kname) print(kp.Name) print('test2') kp:Kick(kreason) end)

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