Help with changing players health

im trying to make it so if a player enters a passcode incorrectly they get shot. this is the script

location: inside local scritpt inside gui

script.Parent.MouseButton1Click:Connect(function(plr)
	if script.Parent.Parent.TextBox.Text == "v" then
		script.Parent.Parent.Parent.Error.Text = "Password Accepted. Welcome"
		script.Parent.Parent.Parent.Error.TextColor3 = Color3.new(0, 1, 0.14902)
	else script.Parent.Parent.TextBox.Text = " "
		script.Parent.Parent.Parent.Error.TextColor3 = Color3.new(1, 0, 0.0156863)
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = "Incorrect Password. Access Denied"
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = " "
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = "Incorrect Password. Access Denied"
		wait(0.5)

		local ui = script.Parent.Parent.Parent.Parent.Die
		local menu = script.Parent.Parent.Parent.Parent.Menu


		print("hit")

		script.Parent.Gunshot:Play()
		
		local player = game:GetService("Players")
		local humanoid = player:WaitForChild("Humanoid")

		humanoid.Health = 0

		ui.Visible = true
		local Death2 = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Death2")
		wait(0.01)
		Death2.Visible = true
		ui.Visible = false

		wait(1)
		plr:Kick("You Died")

		print("everything good")
		print("HOPE")
	end
end)

error: not able to change players health through local script.

Changes to the health property of a Humanoid instance will not replicate to the server if done from the client, you’ll need to use a RemoteEvent to fire the server and then in some server script perform the health deduction.

1 Like

this is not correct. the humanoid is inside the player character.

add ‘.LocalPlayer’ to that.
If you really wanna do it from the client, destroy the player’s head. That replicates to the server. if you want to kill the player via a local script do this:

local player = game:GetService('Players').LocalPlayer
local character = player.Character
local head = character:FindFirstChild('Head')
head:Destroy() --death by beheading, funny

Or do what Forummer said, which is a lot more practical.

1 Like

does this have to be a script or a local script cause I am having this issue to

Okay so basically you need to understand client vs server

Client: The individual player (local script)
Server: All players (script)

Game.Players.LocalPlayer

refers to the individual player. Since the server is all players and not individual players it doesnt know what the individual player is. You can tell it what player to affect by saying

Game.Players.(playername)

If you do want to use Game.Players.LocalPlayer you would have to do it on the client. (remember the client is a local script) This is because, since the client is every individual player, it knows what you are talking about. The game clones the local script to every player that joins, so every player that joins has a different local script with data thats important to them specifically. (like their player properties)

The reason I see in my code why I wasn’t able to change the players health was because I was trying to say Player.Health which health is not a property of player. Instead what I should have done is say

Player.Character.Humanoid.Health

Saying game.Players.LocalPlayer gets the player model inside of the players tab
image

But saying game.Players.LocalPlayer.Character gives me the players character which is located inside of workspace
image

The humanoid is a child of the players character inside of workspace so you have to reference Player.Character then you say
Player.Character.Humanoid.
Health is indeed a property of every humanoid
image
image

so you say for local scritps:

game.Players.LocalPlayer.Character.Humanoid.Health = (Any amount)

and for scripts

game.Players.(playername).Character.Humanoid.Health = (Any amount)
1 Like

@Ma7adi805 Tell me if this helps. Sorry for being rude earlier, I thought you were someone who was responding to my post in terms of trying to help me, not get help.

Uploading: image.png(1)…

that is the error I am getting from the script you gave me and it is local script

Looks Like The image is not uploading so I will be saying it by text

20:29:30 -- Humanoid is not a valid member of Model "Workspace.Ma7adi805"
            Stack Being
            Script 'Players.Ma7adi805.PlayerGui.Settings.Settings2.confirmication.Yes.LocalScript', Line 6

that means the humanoid doesnt exist because the game hasnt loaded yet. Put a wait() or something