Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

? I want it so when I touch it, it takes away 1 and then updates the health bar to the value.

script:

local part = script.Parent

local function HealthLose() game.Workspace.Health.HealthValue = -1 script.Parent.text = game.Workspace.Health.HealthValue end

part.Touched:Connect(HealthLose)

2 comments

0

Do you mean update a custom health bar you made?

0

yeah

1 answer

Code:

-- Server Code

local part = script.Parent
local debounceTable  = {}

local function HealthLose(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player then
		if debounceTable[player.Name] == false then
			debounceTable[player.Name] = true
			game.Workspace[player.Name].Humanoid.Health = game.Workspace[player.Name].Humanoid.Health - 1
			wait(1)
			debounceTable[player.Name] = false
		end
	end
end

part.Touched:Connect(HealthLose)


-- Client Code

game.Players.LocalPlayer.Character.Humanoid.HealthChanged:Connect(function(newHealth)
	HealthBar.Size = UDim2.new(newHealth / 100, 0, 1, 0)
end)

I'm sorry if this is confusing, but it's not possible to make it easier.

If you don't understand the code I recommend learning about debounce, functions like GetPlayers() or :GetPlayerFromCharacter(), guis, and finally server/client relationship.

I hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.