So I have a GUI that if you enter a name and click a Text Button, it will the kill the person whose name you typed in. Here is my code:
01 | script.Parent.Activated:Connect( function () |
02 |
03 | for i,v in pairs (game.Players.LocalPlayer(script.Parent.Parent.person)) |
04 | do |
05 |
06 | v.Character:BreakJoints() |
07 |
08 | end |
09 |
10 | end ) |
The text button and all that works but it does not get the player to kill.
How can I fix this??
add a remote event in replicated Storage; then do this
– from a local script of the player that will type the person to kill
1 | local textBox = -- put here the directory of the textBox where the player types the name of player to kill |
2 | local remoteEvent = --put the directory of where the remote event in replicated storage is |
3 | textBox.FocusLost:Connect( function () |
4 | remoteEvent:FireServer(textBox.Text) |
5 | end ) |
the above code listens for when the player’s cursor stops focusing on the textbox and fires the remote event to let the server know the player to kill. however, if you want the player to click a button then the server is alerted of who to kill then change textBox.FocusLost
to
1 | [ directory of the button to press ] .MouseButton 1 Click |
and that should do it, and dont forget to remove the brackets too
– in server script
1 | local remoteEvent = --put the directory of where the remote event in replicated storage is |
2 | remoteEvent.OnServerEvent:Connect( function (player,toKill) |
3 | for i, plr in pairs (game.Players:GetChildren()) do |
4 | if plr.Name = = toKill then |
5 | plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - plr.Character.Humanoid.MaxHealth |
6 | end |
7 | end |
8 | end ) |
-Insert a RemoteEvent
in the ReplicatedStorage
named "RemoteEvent1"
and another one named "RemoteEvent2"
, and a Script
in the ServerScriptService
. Insert the LocalScript
inside the TextBox
and another in the TextButton
.
–Script in ServerScriptService.
01 | local RemoteEvent 1 = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent1" ) |
02 | local RemoteEvent 2 = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent2" ) |
03 | local Players = game:GetService( "Players" ) |
04 |
05 | local function OnServerEvent 1 (Player, Text) |
06 | Player.PlayerGui.ScreenGui.TextBox.Text = Text |
07 | --Change the ".ScreenGui.TextBox.Text" to the place where the textbox is. |
08 | end |
09 |
10 | local function OnServerEvent 2 (Player, Text) |
11 | local Player = Players [ Text ] --Finds the Player |
12 | if Player then --Making sure that the Player exists. |
13 | local Character = Player.Character |
14 | if Character then --Making sure that the Character exists |
15 | Character:BreakJoints() --Killing the Character. |
16 | end |
17 | end |
18 | end |
19 |
20 | RemoteEvent 1 :Connect(OnServerEvent 1 ) |
21 | RemoteEvent 2 :Connect(OnServerEvent 2 ) |
–LocalScript in TextBox.
1 | local RemoteEvent 1 = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent1" ) |
2 |
3 | local function InputEnded () |
4 | local Text = script.Parent.Text |
5 | RemoteEvent 1 :FireServer(Text) |
6 | end |
7 |
8 | script.Parent.InputEnded:Connect(InputEnded) |
–LocalScript in TextButton.
1 | local RemoteEvent 2 = game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent2" ) |
2 | local function MouseButton 1 Click () |
3 |
4 | local Text = script.Parent.Parent.TextBox.Text --You might want to change this. |
5 | RemoteEvent 2 :FireServer(Text) |
6 | end |
7 |
8 | script.Parent.MouseButton 1 Click:Connect(MouseButton 1 Click) |