Hello, And i’m making a safe zone it all works apart from 1 simple issue that issue is it makes the health to 100 and when your out it stays until you get damaged health but I also have med kits And i don’t want people to abuse the safe zone system for free
Here is my code:
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humanoid.MaxHealth = math.huge
humanoid.Health = math.huge
end
end)
script.Parent.TouchEnded:Connect(function(left)
local character = left.Parent
local humannoid = character:FindFirstChild("Humanoid")
if humannoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humannoid.MaxHealth = 100
humannoid.Health = 100
end
end)
Store the player’s health when they enter and give it back when they leave, Use a table since multiple players will need to have their health stored.
local safePlayerOldHealths = {}
-- on enter
if humanoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
safePlayerOldHealths[character.Name] = humanoid.Health
humanoid.MaxHealth = math.huge
humanoid.Health = math.huge
end
-- on leave
if humannoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humannoid.MaxHealth = 100
if safePlayerOldHealths[character.Name] then
humannoid.Health = safePlayerOldHealths[character.Name]
safePlayerOldHealths[character.Name] = nil
else
humannoid.Health = 100
end
end
we could use Instance Attributes to tell the damage script if its OK to damage the player
-- when they enter the safe zone
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid == nil then return end
humanoid:SetAttribute("Safe", true)
-- when they exit the safe zone
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid == nil then return end
humanoid:SetAttribute("Safe", nil)
-- when someone attacks another player
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid:GetAttribute("Safe") == true then return end -- if the player is safe then exit and dont damage the player
local humanoid = character:FindFirstChild("Humanoid")
local safePlayerOldHealths = {}
if humanoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humanoid.MaxHealth = math.huge
humanoid.Health = math.huge
end
end)
script.Parent.TouchEnded:Connect(function(left)
local character = left.Parent
local humannoid = character:FindFirstChild("Humanoid")
if humannoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humannoid.MaxHealth = 100
humannoid.Health = 100
end
end)
-- on enter
if humanoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
safePlayerOldHealths[character.Name] = humanoid.Health
humanoid.MaxHealth = math.huge
humanoid.Health = math.huge
end
-- on leave
if humannoid ~= nil and game.Players:GetPlayerFromCharacter(character) then
humannoid.MaxHealth = 100
if safePlayerOldHealths[character.Name] then
humannoid.Health = safePlayerOldHealths[character.Name]
safePlayerOldHealths[character.Name] = nil
else
humannoid.Health = 100
end
end
No the comments posted are to help with placement, the full script might look like this but I do not know your exact set up you still may have to apply it differently. I am using @5uphi’s forcefield recommendation, you can replace the Force Field lines with what every method you choose to apply.
script.Parent.Touched:Connect(function(other)
local character = other.Parent
if character:IsA("Model") and character:FindFirstChildOfClass("ForceField") == nil then
Instance.new("ForceField", character) -- give player a force field
end
end)
script.Parent.TouchEnded:Connect(function(other)
local character = other.Parent
local forceField = character:FindFirstChildOfClass("ForceField")
if forceField then
forceField:Destroy() -- remove player's force field
end
end)