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

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
noammao 294 Moderation Voter
4 years ago
Edited 4 years ago

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?)

0
oof I forgot to delete line 3 lol WyattagumsBackUp 5 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Answer this question