Horror Game Kill-Bind help

  1. What do you want to achieve? Killing a player when they press a key (‘F’) in a certain range of a part. Basically a grotty way of saying “Hey! You pressed the bind to enable the flashlight in this area, so we’re going to kill you!” So bare with me, as I am terribly new at scripting.

  2. What is the issue? I do not know if there is a problem with the variables itself or the range but when I playtest it, it doesn’t work as intended. Do I need to fire a client event or leave that as it is?

  3. What solutions have you tried so far? I looked for kill-bind tutorials on Developer Hub and YouTube but couldn’t find any. I also tried changing the position magnitude (as if that was the problem) but to no avail. There doesn’t seem to be an error message in the output which confuses me. Keep in mind I am new to scripting (I am just a builder) so if you could, a summarization and a solution (if possible) can be helpful

local HumanoidRootPart = game:GetService("Players").LocalPlayer:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local part = workspace.FlashlightPart


UserInputService.InputBegan:connect(function(key)
	print(HumanoidRootPart.Position)
	if key.keyCode == Enum.KeyCode.F and (part.Position - HumanoidRootPart.Position).magnitude <= 50 then
		Humanoid.Health = 0
	end
end)
local HumanoidRootPart = game:GetService("Players").LocalPlayer:WaitForChild("HumanoidRootPart")

You forgot to add .Character, try this:

local Players = game:GetService("Players")
local Character = Players.LocalPlayer or Players.LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

Also, this is unrelated to the issue but use :Connect instead of :connect

The two scripts are non-eligible, and pressing the keybind in-range still doesn’t work, I have a suspicion it might be related to the part alone

local Players = game:GetService("Players")
local Character = Players.LocalPlayer or Players.LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")


UserInputService.InputBegan:Connect(function(key)
	print(HumanoidRootPart.Position)
	if key.keyCode == Enum.KeyCode.F and (part.Position - HumanoidRootPart.Position).magnitude <= 50 then
		Humanoid.Health = 0
	end
end)

With ‘part’ and ‘UserInputService’ (missing variable)

local Players = game:GetService("Players")
local Character = Players.LocalPlayer or Players.LocalPlayer.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local part = workspace.FlashlightPart


UserInputService.InputBegan:Connect(function(key)
	print(HumanoidRootPart.Position)
	if key.keyCode == Enum.KeyCode.F and (part.Position - HumanoidRootPart.Position).magnitude <= 50 then
		Humanoid.Health = 0
	end
end)

The code below works correctly for me.


local Players = game:GetService("Players")
local _Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")

local Player = Players.LocalPlayer

local Part = _Workspace:WaitForChild("Part")

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if GameProcessed then return end
	local Character = Player.Character
	
	if Character and Input.KeyCode == Enum.KeyCode.F then
		if (Part.Position - Character.PrimaryPart.Position).Magnitude <= 32 then
			Character.Humanoid:TakeDamage(Character.Humanoid.Health)
		end
	end
end)

I don’t think there is much complex code. So you can understand the variables easily.
But don’t forget that if you will make a radius for this code then the distance will be the half of radius because the center will be away half of the radius from the edge.

1 Like

Thanks. I had to put it in ‘StarterCharacterScripts’ for it to work.

It should be placed in the StarterPlayerScripts because scripts inside StarterCharacterScripts are copied in character every respawn.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.