How to insert a target players name into a line of code after finding it


local tool = script.Parent

tool.Equipped:connect(function(mouse)
	tool.Activated:connect(function()
		if mouse.Target then
			local c = mouse.Target.Parent
			if c:FindFirstChild('Knife') then
				local name = c.Name
				game.Players[name].Character.Humanoid.Health = 0
			
			end
		end
	end)
end)
	
		
			
				
				
				


Hello, I am new to scripting, so sorry if this is confusing. Right now, I am trying to make a tool so when you click someone with a knife in their hand they die. Im doing pretty good up until line 10. I couldn’t figure out how to insert the name of the player into the line of code at 10. If you could figure this problem out that would help me out a ton.

Merry Christmas!

1 Like

You can just use the game:GetService("Players"):GetPlayerFromCharacter(charactermodelhere) method. Also, try to use :GetService() instead of just getting the services directly from the game datamodel.

I edited the script, but it still gives me a stack end in the output. Im trying to kill the opposite player with a localscript in a tool, and I cannot get it to work at all.


local tool = script.Parent

tool.Equipped:connect(function(mouse)
	tool.Activated:connect(function()
		if mouse.Target then
			local c = mouse.Target.Parent
			if c:FindFirstChild('Knife') then
				local name = game:GetService("Players"):GetPlayerFromCharacter(c)
				local player = game.Players[name]
				player.Character.Humanoid.Health = 0
				
			
			end
		end
	end)
end)
	
		

Using a LocalScript to affect another player is a bad idea. The other players and the player you’re affecting will not see the effect because it is all happening locally in a LocalScript. You should use a remote event in order for the other players to see you damaging the other player.

Yeah, I tried that but it doesnt carry the information of the opponent over from the script.

And you should also define the other player’s character and humanoid because they may not exist and your code may break. You can define the character with this line:

local Character = player.Character or player.CharacterAdded:Wait()

Then for the Humanoid, you should wait for it by using :WaitForChild().

How did you try to carry the information over? Remote events have a funny thing happen where the instances that are carried over usually return nil but it can be fixed very easily.

You might have also forgotten to add the player parameter…


local killRemote = script.Parent:WaitForChild("Kill") 

killRemote.OnServerEvent:Connect(function(player)
	
end)

Okay, I edited and made a local, normal script, and added a remote event. I dont know where to go from here. I want to add the c = mouse.Target.Parent from the local script, so I can easily do c.Humanoid.Health = 0 .

You can carry the Humanoid variable on to the remote event script and then just change the Humanoid’s health in the remote event script.

how can I do that? I tried looking into doing that, but I didnt know how to implement it with my variable.

You fire the remote event in the local script and you put the Humanoid variable in the parenthesis.

Then in the server script, add the Humanoid variable in the parenthesis after the player argument as well. You can rename it in the server script as whatever you want but you don’t have to.

Of course you need to define the humanoid variable first:

local Humanoid = Character:WaitForChild("Humanoid")

Alright I did both of those, but doesn’t that just kill you and not the player you clicked on?


local tool = script.Parent

tool.Equipped:connect(function(mouse)
	tool.Activated:connect(function()
		if mouse.Target then
			local c = mouse.Target.Parent
			if c:FindFirstChild('Knife') then
				local name = game:GetService("Players"):GetPlayerFromCharacter(c)
				local player = game.Players[name]
				local Humanoid = c:WaitForChild("Humanoid")
				local killRemote = script.Parent:WaitForChild("Kill") --Requires a RemoteEvent named "KillRemote" to be childed under ReplicatedStorage

				killRemote:FireServer()
			
			end
		end
	end)
end)
	
----------------------------------------------------------		

local killRemote = script.Parent:WaitForChild("Kill") 

killRemote.OnServerEvent:Connect(function(player)
	local Humanoid = player:WaitForChild("Humanoid")
end)			
	

Did it? If it did then I must not understand your code properly. I think the c variable is defined as your player’s character. Here’s a little tip: Make your code understandable and easily readable, I guarantee you that you’ll regret having messy variable names in the future.

Break down the logic in your code as I do not really understand it. I think for now you have to solve the problem.

Okay, I added notes too it and rerote the confusing variables. If you dont know what to do illl keep trying to find solutions.

local tool = script.Parent

tool.Equipped:connect(function(mouse)     
	tool.Activated:connect(function()   
		if mouse.Target then
			local SelectedPlayer = mouse.Target.Parent
			if SelectedPlayer:FindFirstChild('Knife') then  -- checks if clicked player has a knife
				local name = game:GetService("Players"):GetPlayerFromCharacter(SelectedPlayer)
				local player = game.Players[name]             -- gets players name
				local Humanoid = SelectedPlayer:WaitForChild("Humanoid")
				local killRemote = script.Parent:WaitForChild("Kill") --Requires a RemoteEvent named "KillRemote" to be childed under ReplicatedStorage

				killRemote:FireServer() -- fires server to then kill the player with the knife
			
			end
		end
	end)
end)

I forgot to ask, are there any errors displayed in the output? There are also many things I could do differently but it seems to me that SelectedPlayer is actually your character. Maybe try printing the player’s name out?

just make a check to see if SelectedPlayer has a humanoid, if it does then it’s a character and from there you can easily get the name

I gotta go to sleep now but once I wake up and your thread still isn’t solved then I’ll try to continue helping you find a solution. At the moment you can try to debug your code, find similar topics online to get a solution and answer the questions I asked.

Characters always have humanoid, it provides the character its functionality (walking, jumping, etc…).

I know that, that’s why I said make a check to see if mouse.Target.Parent has a humanoid?