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

How can i make player die when player presses m?

This is the local script, and I am confused with line 6.

player = game.Players.LocalPlayer
tool = script.Parent
tool.Equipped:connect(function(mouse)
    mouse.KeyDown:connect(function(key)
	if key == "m" then
		
	end
end)
end)

Please help me.


Purpose : Make player die when player presses m while equipping tool. Confused with: Line 6. I don't know what should i do with LocalPlayer

3 answers

Mouse.KeyDown is deprecated. Please do not use it!

Please use ContextActionService instead!

Here is an example:

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

local player = Players.LocalPlayer
local tool = script.Parent

function onKeyPress(actionName, userInputState, inputObject) -- The new way we are detecting input
	if actionName == "M" and userInputState == Enum.UserInputState.End then -- Fire when they let go of M
		player.Character:BreakJoints()
	end
end

local function ToolEquip(mouse)
	ContextActionService:BindActionToInputTypes("M", onKeyPress, false, Enum.KeyCode.M)
end

local function ToolUnequip()
	ContextActionService:UnbindAction("M")
end

tool.Equipped:connect(ToolEquip)
tool.Unequipped:connect(ToolUnequip)

1 comment

0

You may win this time, but Narrev. I have an edit button and I'm not afraid to use it!

Add this line to your script at line 06

Grabs the character from the player and sets the humanoid's state to dead.

Player.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Dead)

1 comment

0

"player" should be lowercase in order to match the original post. However, that's a pretty cool line of code that I haven't seen before

Another way to go about it, if you're wondering. Explanation's pretty self-explanatory.

local tool = script.Parent
local player = game:GetService('Players').LocalPlayer

tool.Equipped:connect(function(mouse) -- When the player equips tool
	mouse.KeyDown:connect(function(key) -- When a key is pressed
		if string.byte(key) == 109 then -- checks to see if "m" is pressed
			player.Character:BreakJoints() -- Effectively kills the player
		end
	end)
end)

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