Group rank script [Help]

I made a button in my game that when you click it, changes your team in-game. For the change to work, you must be in a certain group. All of it works except for line 4 which is suppose to kill you.

Script:

function Click(mouse)
	if script.Parent.Parent.Parent.Parent.Parent:IsInGroup(5942671) then
		script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Dark stone grey")
		script.Parent.Parent.Parent.Parent.Character.Humanoid.Health = 0
	end
end

script.Parent.MouseButton1Click:connect(Click)

You must use a RemoteEvent to communicate with the server. Changes made by the client are not replicated to the server.

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeTeam = ReplicatedStorage:WaitForChild("ChangeTeam")

function Click(mouse)
	ChangeTeam:FireServer()
end

script.Parent.MouseButton1Click:connect(Click)

Server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player)
	if player:IsInGroup(5942671) then
		player.TeamColor = BrickColor.new("Dark stone grey")
		if player.Character then
			local Humanoid = player.Character:FindFirstChild("Humanoid")
			if Humanoid then
				Humanoid.Health = 0
			end
		end
	end
end)

Why would I have to use a remote event to kill someone? I’m using a server script btw

Fixed it.

function Click(mouse)

if script.Parent.Parent.Parent.Parent.Parent:IsInGroup(5942671) then

script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Dark stone grey")

end

end

script.Parent.MouseButton1Click:connect(Click)

--

function onButtonClicked()

script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.Health = 0

end

script.Parent.MouseButton1Click:connect(onButtonClicked)

That’s not a very efficient way of going about this – I recommend using @Melonishy’s solution instead.

With how they wrote the program, it would only kill the player if they were in the group and joined the team. It’s in the function being called by the RemoteEvent because that’s where the changing and verifying of the player’s handled in the program they wrote.