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

Kick gui script not working,serverscriptservice error?

Asked by 3 years ago
Edited 3 years ago

code:

game.ReplicatedStorage.Events.Kick.OnServerEvent:Connect(function(player)
    local reason = player.PlayerGui.ScreenGui.KickGui.Reason.Text
    local text = reason
    local playerID = player.UserId
    local PLER = player.PlayerGui.ScreenGui.KickGui.Player.Text
    local y = game.Players:FindFirstChild(PLER)
    y:Kick(reason)
end)

error: 18:20:25.551 - ServerScriptService.Script:7: attempt to index nil with 'Kick'

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The reason why this wouldn't work is that the Text Input is client-sided so it won't be able to find the player. What you would have to do use multiple parameters in the firing event, and then call them with OnServerEvent.

Client:

local submitButton = DefineTheKickButtonHere
local playerToKick = DefineTheTextBoxHere
local reason = DefineReasonTextBoxHere
local event = DefineEventHere

submit.MouseButton1Click:Connect(function()
    event:FireServer(playerToKick.Text, reason.Text)
end)

Server:

local event = DefineEventHere

event.OnServerEvent:Connect(function(plrWhoFired, plrToKick, reason)
    if game.Players:FindFirstChild(plrToKick) then
        game.Players:FindFirstChild(plrToKick):Kick(reason)
    end
end)

Side-Note: the first parameter of the OnServerEvent event is the player who fired it

0
you are saying "game.Players:FindFirstChild(plrToKick)" two times, use a variable instead CaioAlpaca 342 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

There's a better way on doing this:

You wanna use RemoveEvents' argument.

Script on local script:

local kickButton = -- put where is your kick button
local playerToKick = -- put where is your player to kick label
local kickReason = -- put where is your reason text label

local RS = game:GetService("ReplicatedStorage")

kickButton.MouseButton1Up:Connect(function()
    local KickEvent = RS:WaitForChild("KickPlayer")
    KickEvent:FireServer(playerToKick, kickReason) -- passing out 2 arguments (player and reason)
end)

Script on server script:

local RS = game:GetService("ReplicatedStorage")

RS.KickPlayer.OnServerEvent:Connect(function(_, player, reason) 
    game.Players:FindFirstChild(player):Kick(reason)
end)

Let me know if it works!

0
I just realized somone posted before me. Sorry lol Feelings_La 399 — 3y

Answer this question