I’m making a city game, and there’d be a gate that’d let teams pass, and other teams don’t. However, you are able to damage/destroy the gate. I don’t know how to go about making a health system, I have the health gui down already, and the localscript for the passing aspect. Any help is appreciated!
localscript code (the localscript is in playerscripts)
local player = game.Players.LocalPlayer
local teams = game:GetService("Teams")
local door = game.Workspace.Gate
while true do
if player.Team == teams.Civilian or player.Team == teams.Leader or player.Team == teams.Admission then
door.CanCollide = false
else
door.CanCollide = true
end
wait(0.1)
end
I have an idea, and I have a better idea. I’m going to tell you the first idea, because I don’t have enough time left to explain the other idea. So basically put a Humanoid inside the gate part, and make a script like this:
local damage = 0.01
local Gate = game.Workspace.Gate
local humanoid = Gate.Humanoid
local ProgressBar = Gate.TouchPart.BillboardGui.ProgressBar
while true do
ProgressBar.Size = UDim2.new(humanoid.Health/100, 0, 1, 0)
if humanoid.Health <= 0 then
Gate:Destroy()
end
wait()
end
Gate.TouchPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
humanoid.Health -= damage
end
end
end)
This is just a simple idea, and I know there will be an error if you copy the exact code, but it’s just a simple idea to get you started. Sorry, I didn’t get to show you the more efficient way.
I tested it out. It did not work for some reason, the humanoid health is not decreasing. I wanted it to be destroyed with tools like swords or bombs. When the gate gets destroyed, I don’t want it be completely gone, so it looks like it’s gone, but it’s not.
To do that when the gate goes below a certain health threshold, the gate model could be replaced by a more damaged gate model. You could even have multiple health stages of the gate to show destruction over time.
Insert a Humanoid instance into the gate model as suggested and give its properties your own desired values. You will then need to make a script which can allow for the gate to receive damage.
Alright, I rewrote the entire script, but it isn’t working. I inserted a numbervalue, and tested a sword. I don’t know what’s wrong with it.
local gate = script.Parent
local health = gate.Value
gate.Touched:Connect(function(hit)
if hit:IsA("Tool") then
if hit.Name == "Sword" or hit.Name == "ClassicSword" then
health.Value = health.Value - 10
end
end
end)
local gate = script.Parent
local health = script.Parent.Humanoid.Health
gate.Touched:Connect(function(hit)
if hit:IsA("Tool") then
if hit.Name == "Sword" or hit.Name == "ClassicSword" then
health = health - 10
end
end
end)
Assuming the Humanoid instance is a child of the gate part.
gate.Touched:Connect(function(hit)
if hit:IsA("Part") or hit:IsA("MeshPart") then
if hit.Parent:IsA("Tool")
if hit.Parent.Name == "Sword" or hit.Parent.Name == "ClassicSword" then
health.Value = health.Value - 10
end
end
end
end)
yeah but a problem with that is even if the sword just touches it without swinging it damages the gate so i hope you have a script that detects swings…
I have literally no idea on how to detect swings. I plan on adding a debounce so they don’t hold the sword at the wall and break it in one second. I would like to know how to make a swing detecting script, however.
local gate = script.Parent
gate.Touched:Connect(function(hit)
if hit:IsA("Part") then
if hit.Parent:IsA("Tool") then
if hit.Parent.Name == "Sword" then
script.Parent.Humanoid.Health = script.Parent.Humanoid.Health - 10
end
end
end
end)
Just a more refined solution using the specific tool you linked. Is gate.Value an IntValue instance? You only need a Humanoid instance with the above implementation.
The variable health doesn’t change with the humanoid’s health property; it is a flat number value of the humanoid’s health at the time it was defined, meaning it cannot actually be used to damage the gate. See if this works instead:
local gate = script.Parent
local humanoid = script.Parent.Humanoid
gate.Touched:Connect(function(hit)
if hit:IsA("Part") then
if hit.Parent:IsA("Tool") then
if hit.Parent.Name == "Sword" then
humanoid.Health = humanoid.Health - 10
end
end
end
end)
EDIT: Here’s a shorter and more efficient way to write this:
local gate = script.Parent
local humanoid = script.Parent.Humanoid
gate.Touched:Connect(function(hit)
if hit.Parent:IsA("Tool") and hit.Parent.Name == "Sword" then
humanoid.Health -= 10
end
end)
I was using the variable as a quick storage for the gate’s health and forgot to inflict the damage to the gate when appropriate, I’ve made the slight modification in my code already.