Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago

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

Ad
Log in to vote
0
Answered by 10 years ago

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

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 TheRings0fSaturn 28 — 10y

Answer this question