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

?General KeyDown() Function needs help :D

Asked by 9 years ago
local Player = game.Players.LocalPlayer

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if string.lower(key) ==  "r" then
    Human = game.Workspace:FindFirstChild(Player)
    Human.Humanoid.Health = 0
    end
end)

I don't necessarily need this for "r" or resetting the player...I'd just like to know how to access the player in workspace when a button is pressed.

3 answers

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

:FindFirstChild() accepts a string, not an object.

The Player variable is an object, because it is referring to game.Players.LocalPlayer.

So instead of this:

Human = game.Workspace:FindFirstChild(Player)

Syntactically, the correct way is this:

Human = game.Workspace:FindFirstChild(Player.Name)

I do not recommend you to access the player in this manner, because in some cases there could be names of objects that are the same as the Player's name in the Workspace.

Assuming that your script is a localscript, you can access the Character with simply:

game.Players.LocalPlayer.Character

Therefore, the best way to access the character model is this:

Human = Player.Character

Since a variable takes the place of something else, you do not need to write the whole name of the player model.

Also, I'd refer the player's mouse with a separate variable, so it would look cleaner.

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

Mouse.KeyDown:connect(function(key)
    if string.lower(key) == "r" then
        local Human = Player.Character
        Human:WaitForChild("Humanoid").Health = 0
    end
end)
0
. BSIncorporated 640 — 9y
0
its still not working, on a side note...i know now to do Player.Character to get from Players to workspace more efficiently, how can I script Character to player efficiently? BSIncorporated 640 — 9y
0
Using "game.Players:GetPlayerFromCharacter(InsertCharacterHere)" Redbullusa 1580 — 9y
0
If it's not working, then, in line 1, add an entry like so: "repeat wait() until game.Players.LocalPlayer" Redbullusa 1580 — 9y
View all comments (2 more)
0
Be sure that the code is a LocalScript, that "Disabled" is set to false, and that the script is in StarterGui. Redbullusa 1580 — 9y
0
Ohhh, I didn't have it in StaterGui, thank you so much for your help BSIncorporated 640 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

I would suggest using InputBegan of UserInputService instead of KeyDown.

Localscript:

local UserInput = Game:GetService("UserInputService")

UserInput.InputBegan:connect(function(Input) --Equivalent to keydown.
    if Input.KeyCode == Enum.Keyboard.R then
        Game.Players.LocalPlayer.Character:BreakJoints() --Killing the player by breaking joints.
    end
end)
Log in to vote
0
Answered by 9 years ago
local Player = game.Players.LocalPlayer:GetMouse()--This get's the mouse of your current LocalPlayer in players.
function KeyDown(key)--We create a parameter
if key:lower() == "r" then--We create any lower case key here we want to use.
Human = game.Players.LocalPlayer.Character.Humanoid---A variable to make it simple.
Human.Health = 0--Killing the player afterwards.
end
end
Player.KeyDown:connect(KeyDown)

Answer this question