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

Why wont this kill button function properly?

I am making an admin script with a player name box and it will do whatever command to the players who's name is in the box. This particular one is a kill script, I can't seem to figure out how to make it work.

I think the problem is that it requires a remote, but I'm afraid to use one because I don't want it to be exploited.

script.Parent.MouseButton1Click:Connect(function() --Kill button
	local player = script.Parent.Parent.Player.Text --Player name in the box
	for i,v in pairs(game.Players:GetChildren()) do 
		if player == v then
			v.Character.Humanoid.Health = 0
		end
	end
end)

6 comments

0

Remotes help prevent it from being exploited... At least while you had the option of FE or not they were better than the alternative.

0

Remotespy makes it bad though

0

have the server check who fired the event and ban them if they aren't an admin

0

I'm still sort of new to scripting, so I have no idea how to protect my remotes. ;-;

0

it really comes down to the server checking if the client is legit about what event they're sending

0

And how would I do that?

2 answers

✓ Accepted answer

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") -- Waits for the remote event.

script.Parent.MouseButton1Click:Connect(function() --Kill button
	local player = script.Parent.Parent.Player.Text --Player name in the box
	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 -- kills the target
	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 -- kills the target
		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)).

7 comments

0

Don't remind users to accept your answer. It is nature for the asker to accept the answer. You're just being greedy. Do not give deprecated code.

0

What part of the code was deprecated besides capitalizing the C in connect? I have noted your previous comment, you do not have to be hostile.

0

The parent argument to Instance.new.

0

But the problem with this is I have no idea how to protect my remotes, so all an exploiter would need is a remotespy, and its game over.

0

The parent argument to Instance.new.

0

@incapaz I have updated the code, there should be no deprecated code.

0

@DominousSyndicate The third piece of code I supplied will only kill the target player if the player who fires the remote has his or her username whitelisted in the table. Random players trying to fire the event will not be able to kill anyone.

There is no point in cycling through every player. Also, if you were to use this method, line 4 should read:

if v.Name = player then

Your code is checking if the text is the same as the player, not if the player's name is the same as the text. A player is not equivalent to text. However, I don't recommend the method you are using in the first place. Use this instead:

script.Parent.MouseButton1Click:Connect(function()
    local playername = script.Parent.Parent.Player.Text
    if game:GetService("Players"):FindFirstChild(playername) then --Check if the player exists
   	    local player = game:GetService("Players"):FindFirstChild(playername)
	    player.Character:BreakJoints()	
	end
end)

I typed this in here so it may not work. Please tell me if it doesn't and I'll try to fix it. Hope this helps, and if it does, please accept my answer!

10 comments

0

Don't remind users to accept your answer. It is nature for the asker to accept the answer. You're just being greedy.

0

That isn't being greedy. There is really nothing wrong with reminding people to accept answers. Some people don't. 

0

Yes it is...

0

This works, at least in testing mode.I have not tried it in a published game, but if it doesn't work I will be sure to let you know.

0

You raise an interesting point. But should someone who reminds people to accept their answer with a bad answer (this answer wasn't bad) be accepted? I'd argue not.

0

Of course not. If they have posted a bad answer then of course, it shouldn't be accepted. But the reason why they post bad answer is because they believe it is the right answer. Hence they post, "Accept my answer."

0

Thank you for the suggestion in the previous comments, but it would be appreciated if you reply in a friendly manner next time)). @incapaz

0

Just wondering, why are you trying to kill someone's character through a localscript? Pretty sure every game is forced FE so this would only work in studio.

0

Sorry, if I sounded greedy in any way. Some new users who don't understand the website very well may not know to or how to accept an answer, so I usually add that in my answer just to let them know. Also, V_ChampionSSR is correct. I had forgotten to take FE (Filtering Enabled) into consideration, as I am new to the concept. So, you should probably use a RemoteEvent.

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