Trying to prevent script from killing me

i have a loopkill script which has a gui list of plrs but i dont want it to kill my own character. it’s in a script, i’m not sure how to do it. would i have to send a remote event to the client to check if the text name on the GUI is = to my name? ty!!

this is a server script:

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			if findPlr.Character and findPlr.Character:FindFirstChild("Humanoid") then
				if findPlr.Character.Humanoid.Health > 0 then
					findPlr.Character.Humanoid.Health = 0
				end
			end
		end
	end
end)
1 Like

So if you don’t want to kill your character, you can add a statement to check if the name is not yours.
Here is an example:

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			if findPlr.Name ~= 'targetframed' and findPlr.Character and findPlr.Character:FindFirstChild("Humanoid") then
				if findPlr.Character.Humanoid.Health > 0 then
					findPlr.Character.Humanoid.Health = 0
				end
			end
		end
	end
end)

I hope this helps you! :slight_smile:

1 Like

hello! ty 4 ur response man.

while this is great, this will only work for me. i’m trying to make it so that if the person who clicked on themselves to loopkill then to return. great idea tho!

i found this post so ill try this rn:

1 Like

You would need to detect a click on the client, and use a remote event to tell the server. From their you can get the name of the person who triggered the remote event and exclude them.
Here is some code from the server:

remoteevent.OnServerEvent:Connect(function(playerWhoTriggered)
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			if findPlr.Name ~= playerWhoTriggered.Name and findPlr.Character and findPlr.Character:FindFirstChild("Humanoid") then
				if findPlr.Character.Humanoid.Health > 0 then
					findPlr.Character.Humanoid.Health = 0
				end
			end
		end
	end
end)
1 Like

Let me know if this does not work or their is another issue. Thanks.

hey man, hows it going

thanks a million for ur help. just 1 thing. im trying to send a remote event from a server script to a local script and i tried sending the player to the local script but it says “indexing nil with character” for the findPlr

yes.MouseButton1Click:Connect(function()
	plrchoiceUI.Enabled = false
	if selectedButton then
		selectedButton.BackgroundColor3 = unselectedColor
	end
	if selectedButton then
		local findPlr:Player = plrs:FindFirstChild(selectedButton.Text)
		if findPlr then
			plrcheckEvent:FireClient(findPlr)
		end
	end
end)

and local script:

local plrcheckEvent = game.ReplicatedStorage:WaitForChild("plrcheckEvent")

plrcheckEvent.OnClientEvent:Connect(function(findplr)
	if 	findplr.Character.Humanoid.Health > 0 then
		findplr.Character.Humanoid.Health = 0
	end
end)

You can’t pass an instance along a RemoteEvent. Try passing something the player can be identified by, like a name or UserId.

2 Likes

This is incorrect; when passing instances as an argument to remotes you’re actually referring to an existing instance. When doing this, it’s important that both the server and client can index the instance (ex. passing an instance to the server which is created locally will be nil on the server).

By the looks of it, it seems like you’re trying to target a specific player from a player list.
I assume you’re iterating through each player to generate the list, so you should be able to do this:

Client logic

function CreateLabel(player)
	-- create label logic...
	local label = template:Clone()
	-- etc...
	
	-- handle button logic
	local button = label.Button

	button.MouseButton1Click:Connect(function()
		loopKillRemote:FireServer(player)
	end)
end

-- generate list
for _, player in game.Players:GetPlayers() do
	CreateLabel(player)
end

Server logic

local immuneUserIds = {} -- ex. {1} where 1 is Roblox's userid
local playersToLoopKill = {} -- keep track of who to loop kill

loopKillRemote.OnServerEvent:Connect(function(player, targetPlayer)
	-- check if <targetPlayer> is immune
	if table.find(immuneUserIds, targetPlayer.UserId) then return end
	
	-- make sure <player> has the permission to do this
	-- logic here...
	
	-- add to loop kill table
	playersToLoopKill[targetPlayer] = true
	
	-- kill once if alive
	local character = targetPlayer.Character
	if character then
		character.Humanoid.Health = 0
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if playersToLoopKill[targetPlayer] then -- is in loop kill table?
			task.wait(1) -- how long to wait after spawning
			if (not character.Parent) then return end -- player died while we waited
			character.Humanoid.Health = 0
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	-- player has left, remove from loop kill table
	playersToLoopKill[targetPlayer] = nil
end)

Edit: added a check for immune users as the original post asked for this

You’re only passing findPlr. The first arguement of FireClient must be the player object you are trying to fire to. Include the player as the first parameter and the target player as the second.