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

How do i get a player's character in a local script?

Asked by 5 years ago

So im trying to get started with some GUI programming by making a simple button that, when you click it, kills you. However, i ran into some issues in finding a player's humanoid. I've tried many methods from this

Error message #1: Players.SRdeagle.PlayerGui.ScreenGui.TextButton.LocalScript:5: attempt to index upvalue 'humanoid' (a nil value)

Attempt #1:

local button = script.Parent
local plrnm = game.Players.LocalPlayer.Name
local humanoid = workspace[plrnm]:FindFirstChild('Humanoid')
local function pressed ()
    humanoid.Health = 0
end
button.MouseButton1Click:Connect(pressed)

to this

Error message #2: Humanoid is not a valid member of Model

Attempt 2#

local button = script.Parent
local plr = game.Players.LocalPlayer
local character = plr.Character
local humanoid = character.Humanoid
local function pressed ()
    humanoid.Health = 0
end
button.MouseButton1Click:Connect(pressed)

Both of these methods i picked up form solved questions on this site with others having the same issue, but the thing is it apparently worked when they did them. I can see that the issue in most of my attempts is that im trying to get a humanoid out of a model, but it makes no sense to me how i can't do that since i can clearly see a humanoid under a 'SRdeagle' (my roblox username) model in workspace when i start the game...

So yeah, if anyone would be kind enough to answer me it would me much appreciated.

P.S. - I have just started with lua programming as you can probably see

4 answers

Log in to vote
0
Answered by 5 years ago

It's not that your script hasn't found the players character in the workspace, it's that you're trying to change something on the client which should be changed on the server. So this is what you need to do.

Create a RemoteEvent in ReplicatedStorage and call it "ClickKill"

Now create a server script in ServerScriptService with the following code

game.ReplicatedStorage:FindFirstChild("ClickKill").OnServerEvent:Connect(function(Player)
    local character = plr.Character or workspace[Player.Name]
    character:FindFirstChild("Humanoid").Health = 0
end)

Now you need to change your localscript code to the following

local function pressed()
    game.ReplicatedStorage:FindFirstChild("ClickKill"):FireServer()
end

script.Parent.MouseButton1Click:Connect(pressed)

You do not need the player argument for :FireServer() because it by default includes it as the first argument. Keep note of this.

Look up RemoteEvents and RemoteFunctions for cross client-server communication, you will need this in future.

0
Thank you for the long and thorough answer! I will try to do this. Who knew a simple KYS button would be this hard to make SRdeagle 7 — 5y
0
You'll get the hang of it, it gets easier after a while. Shoot me a DM if you need any help with anything. MachoPiggies#3484 MachoPiggies 526 — 5y
0
this is so unnecessary Gameplayer365247v2 1055 — 5y
0
Speak to me when you learn to use spacing MachoPiggies 526 — 5y
View all comments (2 more)
1
Thanks for the kindness, i'll hit you up if i need some help. SRdeagle 7 — 5y
0
If it worked, feel free to mark my post as correct <3 MachoPiggies 526 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

local script:

script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage["kill player"]:FireServer()
end)

server script:

local remote = Instance.new("RemoteEvent",game.ReplicatedStorage")
remote.Name = "kill player"
remote.OnServerEvent:Connect(function(player)
player.Character.Humanoid.Health = 0
end)

easy enough, when you click it fires a remote event to the server that will kill you

0
also make sure the game is set to R15 character or it wont work Gameplayer365247v2 1055 — 5y
0
What would be the advantage in taking health from the server instead of the client? Thetacah 712 — 5y
0
ive just heard from quite a few more experienced scripters that its better to do it on the server instead Gameplayer365247v2 1055 — 5y
0
Way better to do on the server, you'll have to implement server-sided checks if you're doing that. vkax 85 — 5y
View all comments (3 more)
0
Oh, my bad, didn't realize this was a click to kill GUI XD vkax 85 — 5y
0
there is no need to implement some check here since it kills whoever fired the server and its the person who clicked the button that fires it Gameplayer365247v2 1055 — 5y
0
Yeah, I was confused as to why you wanted to do it from the server xD. Thetacah 712 — 5y
Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
5 years ago
Edited 5 years ago

I will use Attempt #2. You should define both the character and the humanoid in the function itself, as it appears your script ran before Humanoid was created in your character. Another way to solve this would be to use the WaitForChild() method.

Your code should look something like this(in a LocalScript, located inside the button):

local button = script.Parent
local plr = game.Players.LocalPlayer

local function pressed ()
    local character = plr.Character or plr.CharacterAdded:wait()
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end
button.MouseButton1Click:Connect(pressed)
0
Thank you! This really works. Honestly ,i'm not sure how that or plr.CharacterAdded:wait() made such a difference but it worked anyways. SRdeagle 7 — 5y
Log in to vote
0
Answered by 5 years ago

local player = game.Players.LocalPlayer player.CharacterAdded:connect(function(character)

end)

Answer this question