I’ve made portals that gives buffs to players whenever they are touched and I’ve created this script for a Max Health Buff portal but I feel like I’ve been reptitively using this script too much. I’m only modifying a few things just for the script to work. I’m trying to learn and use module scripts to avoid repeatedly using the same script over and over but I don’t know how.
-- This is the script I've been repeatedly using
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local MainFrame = script.Parent
local Sounds = ReplicatedStorage:WaitForChild("Sounds")
local Buff_Sound = Sounds:WaitForChild("dio_heal")
local Error_Sound = Sounds:WaitForChild("Windows XP Error")
local Health_Owner = script:WaitForChild("HealthOwner")
local Health_Time = script:WaitForChild("HealthTime")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Portal_Modules = Modules:WaitForChild("Portal_Modules")
local Random_FFA_Spawn = require(Portal_Modules:WaitForChild("Random_FFA_Spawn"))
local Owner_Character = nil
local COOLDOWN_IS_RUNNING = false
local debounce = false
local function Cooldown()
if COOLDOWN_IS_RUNNING == true then return end -- Prevent from running if it's already running
COOLDOWN_IS_RUNNING = true
while COOLDOWN_IS_RUNNING == true and Health_Time.Value > 0 do
if COOLDOWN_IS_RUNNING == false then break end
Health_Time.Value -= 1
task.wait(1)
end
if Health_Time.Value <= 0 and Owner_Character ~= nil then
local Humanoid = Owner_Character:WaitForChild("Humanoid")
local HRP = Owner_Character:WaitForChild("HumanoidRootPart")
if Humanoid and HRP then
Humanoid.MaxHealth = 100
HRP:FindFirstChild("Health Sparkes"):Destroy()
end
Health_Owner.Value = "nil"
Owner_Character = nil
end
COOLDOWN_IS_RUNNING = false
end
MainFrame.Touched:Connect(function(hit)
if debounce == true then return end
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
local character = hit.Parent
local Humanoid = character:FindFirstChild("Humanoid")
local HRP = character:FindFirstChild("HumanoidRootPart")
if Health_Owner.Value == "nil" and Health_Time.Value <= 0 and not HRP:FindFirstChildOfClass("Sparkles") then
Humanoid.MaxHealth = 200
Health_Owner.Value = character.Name
Health_Time.Value = 180
Random_FFA_Spawn.Random_Spawn(character)
local Health_Sparkles = Instance.new("Sparkles")
Health_Sparkles.Name = "Health Sparkles"
Health_Sparkles.SparkleColor = Color3.fromRGB(0, 255, 0)
Health_Sparkles.Parent = HRP
Owner_Character = character
local Buff_Sound_Clone = Buff_Sound:Clone()
Buff_Sound_Clone.Parent = HRP
Buff_Sound_Clone:Play()
Buff_Sound_Clone.Ended:Wait()
Buff_Sound_Clone:Destroy()
else
Random_FFA_Spawn.Random_Spawn(character)
local Error_Sound_Clone = Error_Sound:Clone()
Error_Sound_Clone.Parent = HRP
Error_Sound_Clone:Play()
Error_Sound_Clone.Ended:Wait()
Error_Sound_Clone:Destroy()
end
else
error("Failed to find Humanoid")
end
task.wait(1)
debounce = false
end)
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local Humanoid = character:WaitForChild("Humanoid")
Humanoid.Died:Once(function()
if player.Name == Health_Owner.Value then
COOLDOWN_IS_RUNNING = false
Health_Time.Value = 0
Health_Owner.Value = "nil"
Owner_Character = nil
end
end)
end)
end)
Players.PlayerRemoving:Connect(function(player)
if player.Name == Health_Owner.Value then
COOLDOWN_IS_RUNNING = false
Health_Time.Value = 0
Health_Owner.Value = "nil"
Owner_Character = nil
end
end)