So I have just had a issue this morning in roblox studio about coding kick functions when you type Reasons/Usernames into the TextBox, my code consists of…
function Clicked()
game.Players.(script.Parent.Text):Kick(script.Parent.Parent.Reason.Text)
end
script.Parent.MouseButton1Down:connect(Clicked)
I have coded a script that will… ``` When clicked a GUI… it will look into game.Players and then look into a TextBox to see what player the admin/mod had typed in for the player to kick, and then a reason on what they typed in a different text box, and I am getting a error saying telling me:
But this error is appearing only when i join the server, when i want it to function the script when you click the Kick button as shown in the video below:
If anyone knows what the sullotion could be please leave a comment below because I am making a game, currently developing a Admin Panel for when where Testing. Thanks
You cannot get a child of an object with .string. To use a string for this use brackets.
function Clicked()
game.Players[script.Parent.Text]:Kick(script.Parent.Parent.Reason.Text)
end
script.Parent.MouseButton1Down:Connect(Clicked)
Also, check to make sure the text is a player’s name in the game.
function Clicked()
if game.Players:FindFirstChild(script.Parent.Text) then
game.Players.(script.Parent.Text):Kick(script.Parent.Parent.Reason.Text)\
else
print("Player not found!")
end
end
script.Parent.MouseButton1Down:Connect(Clicked)
We are using brackets because doing () when doing game.Players. will error.
So, using brackets will help.
Also, doing this cliently will not work.
I suggest doing remote events.
Using the remote event as a server script as the reciever, it will be able to kick players from the game.
while locally, it will not work as they do not have permission to.
Client is you for example, and the server is the actual game.
The reason why a client cannot kick another client via a localscript is because it does not replicate to the server.
And, if you attempt to to kick a player if they meet a certain condition, a exploiter can cancel the :Kick() packet being sent to their client.
(ex, if you had a localscript detecting if their WalkSpeed is above a certain number).
Your best bet would be to crash them instead (while true do end).
Use remote events, the server script as the reciever, and the client script as the sender.
Client cannot kick players. Only the server can because they are basically the game itself.
--[[ <-> localscript <-> --]]
local Event = game:GetService('ReplicatedStorage'):WaitForChild('RemoteEvent')
-- your code including:
Event:FireServer(Username, Reason)
--[[ <-> serverscript <-> --]]
local Players = game:GetService('Players')
local Event = game:GetService('ReplicatedStorage').RemoteEvent
Event.OnServerEvent:Connect(function(Player, Username, Reason) -- Player is automatically sent to the server.
--[[
you should probably whitelist the players that is allowed to use this,
as any exploiter can call FireServer() with their own arguments and kick whoever they want.
--]]
local TargetPlayer = Players:FindFirstChild(Username)
if TargetPlayer and Reason:find('%w') then
-- check if the reason contains a alphanumeric character (0-9, a-z)
TargetPlayer:Kick(Reason)
end
end)
Hope this helps.
Keep in mind that you need to create a RemoteEvent inside the ReplicatedStorage service.