To kill other players, you'll need to use a remote event because of the FilteringEnabled rules.
Just a tip for the localscript, you are trying to compare a player instance with a string. On line 4, instead of using if player == v then, use if player == v.Name then, so it compares a string to a string.
For line 5, you'll need to fire off a remote event.
Then you'll need to use a serverscript (which you should put in the ServerScriptService), which retrieves the call from the client and kills the player you want.
Here's some example code.
Local Script:
local Event = game.ReplicatedStorage:WaitForChild("AdminKill")
script.Parent.MouseButton1Click:Connect(function()
local player = script.Parent.Parent.Player.Text
if game.Players:FindFirstChild(player) then
Event:FireServer(player)
end
end)
*side notes: I've updated the code, the other answer was correct about it being unnecessary to cycle through each player.
Server Script
local Event = Instance.new("RemoteEvent")
Event.Parent = game.ReplicatedStorage
Event.Name = "AdminKill"
Event.OnServerEvent:Connect(function(player, target)
if game.Players:FindFirstChild(target) then
local targetplayer = game.Players:FindFirstChild(target)
targetplayer.Character:FindFirstChild("Humanoid").Health = 0
end
end)
If you want it so that only certain players can kill the target players, you could make an admin-whitelist table like so:
local Event = Instance.new("RemoteEvent", game.ReplicatedStorage)
Event.Name = "AdminKill"
local Admins = {"Player1", "Player2", "Player3"}
function CheckIfAdmin(player)
local result = false
for i, v in pairs(Admins) do
if player.Name == v then
result = true
end
end
return result
end
Event.OnServerEvent:Connect(function(player, target)
if game.Players:FindFirstChild(target) then
local isAdmin = CheckIfAdmin(player)
if isAdmin then
local targetplayer = game.Players:FindFirstChild(target)
targetplayer.Character:FindFirstChild("Humanoid").Health = 0
end
end
end)
Hope this helps! If there's an issue or you have a question, let me know in the comments. I've wrote the code via ScriptingHelpers, and not in studio, so let me know if there's any errors by testing it out)).
If you're satisfied, be sure to accept this answer)).
6 comments
Remotes help prevent it from being exploited... At least while you had the option of FE or not they were better than the alternative.
Remotespy makes it bad though
have the server check who fired the event and ban them if they aren't an admin
I'm still sort of new to scripting, so I have no idea how to protect my remotes. ;-;
it really comes down to the server checking if the client is legit about what event they're sending
And how would I do that?