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
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.
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
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)
local player = game.Players.LocalPlayer player.CharacterAdded:connect(function(character)
end)