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

How to hurt a player when they press the letter "K?"

I have a script similar to this, but it does NOT work. Can anyone help me?

function(OnKeyPressed)
Local key = k

if Game.Workspace.Player(OnKeyPressed)
then
Player.Humanoid.Health = - 25
end

2 answers

You use the PlayerMouse event 'KeyDown'. The event is fired when a key is pressed, and you can only get the mouse from a LocalScript.

--Also, just a few things that were wrong with your script.
--Firstly, when defining a local variable, you must have 'local' in lowercase letters.
--Secondly, the Player isn't a child of the Workspace. What you're looking for is the Character, which is the 3D model of your Player. The Player's Character can be accessed through the player though, by the property 'Character'. With getting the Player from the Character, you can use the inbuilt 'GetPlayerFromCharacter' function to get a player from a character model.
--Thirdly, functions run. They're not used in an if statement, if statements are used to see if something exists/is true.
-- And finally, with decreasing values, you have to state what to decrease it from first. Otherwise, you're pretty much saying 'The character's health is equal to subtract 25'

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(key)
	if key == "k" then
		Character.Humanoid.Health = Character.Humanoid.Health - 25
	end
end)

Apologies if this was unhelpful to you, if it was, please downvote and explain why :) Or just downvote if you just think I suck xD

you always need to follow the rules of scripting. Good try and you're doing well. Make sure you write "local" not Local, etc.

1 comment

0

Alright. Thanks! I just learned how to start programming back in December, and I'm still working on it. I've read many scripts from free models, and I can say I've learned a lot. I can't get animations down, though. lol

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