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.
: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)
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)
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)