this code.
--[[
HazardScript.lua
Attribute-driven hazard pad script for Roblox.
Supports: Fling, randomization, debounce, damage types, stun types, and more.
Place this script under a folder containing hazard pads (BaseParts).
Each pad should have relevant attributes set for custom behavior.
]]
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
-- Default configuration (used if attribute is missing)
local DEFAULTS = {
FlingEnabled = true,
FlingPower = 125,
FlingVertical = 25,
FlingRandom = false,
FlingRandomMin = 80,
FlingRandomMax = 180,
RotationAngle = 50,
RotateTime = 0.1,
RestoreTime = 0.5,
DebounceEnabled = true,
DebounceTime = 0.5,
DamageEnabled = false,
DamageType = "none", -- "fire", "ice", "poison", "none"
DamageAmount = 10,
StunEnabled = false,
StunType = "none", -- "none", "light", "heavy"
StunTime = 1.5,
SoundTemplateName = "LaunchSound"
}
-- Helper: Get attribute or fallback to default
local function getAttr(pad, attr, default)
local val = pad:GetAttribute(attr)
if val == nil then
return default
end
return val
end
-- Helper: Apply damage to a character
local function applyDamage(character, amount, dtype)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
humanoid:TakeDamage(amount)
-- Optionally, apply status effect (visuals, etc.)
if dtype == "fire" then
-- Example: Add fire effect
if not character:FindFirstChild("HazardFire") then
local fire = Instance.new("Fire")
fire.Name = "HazardFire"
fire.Heat = 10
fire.Size = 5
fire.Parent = character:FindFirstChild("HumanoidRootPart") or character
Debris:AddItem(fire, 2)
end
elseif dtype == "ice" then
-- Example: Add ice effect (blue tint)
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp and not hrp:FindFirstChild("HazardIce") then
local ice = Instance.new("ParticleEmitter")
ice.Name = "HazardIce"
ice.Texture = "rbxassetid://284205403" -- Example ice texture
ice.Color = ColorSequence.new(Color3.fromRGB(150, 200, 255))
ice.Lifetime = NumberRange.new(0.5)
ice.Rate = 20
ice.Parent = hrp
Debris:AddItem(ice, 1)
end
elseif dtype == "poison" then
-- Example: Add poison effect (green particles)
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp and not hrp:FindFirstChild("HazardPoison") then
local poison = Instance.new("ParticleEmitter")
poison.Name = "HazardPoison"
poison.Texture = "rbxassetid://248625108" -- Example poison texture
poison.Color = ColorSequence.new(Color3.fromRGB(80, 255, 80))
poison.Lifetime = NumberRange.new(0.5)
poison.Rate = 20
poison.Parent = hrp
Debris:AddItem(poison, 1)
end
end
end
-- Helper: Apply stun to a character
local function applyStun(character, stunType, stunTime)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
if stunType == "heavy" then
-- Heavy stun: Set walkspeed/jump to 0, anchor HRP
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
hrp.Anchored = true
Debris:AddItem(hrp, stunTime)
task.delay(stunTime, function()
if hrp then hrp.Anchored = false end
end)
end
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
task.delay(stunTime, function()
if humanoid then
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
end
end)
elseif stunType == "light" then
-- Light stun: Reduce walkspeed/jump, allow movement
local origSpeed = humanoid.WalkSpeed
local origJump = humanoid.JumpPower
humanoid.WalkSpeed = math.max(4, origSpeed * 0.3)
humanoid.JumpPower = math.max(10, origJump * 0.3)
task.delay(stunTime, function()
if humanoid then
humanoid.WalkSpeed = origSpeed
humanoid.JumpPower = origJump
end
end)
end
end
-- Main pad setup
local function setupPad(pad)
local lastUse = 0
pad.Touched:Connect(function(hit)
local now = tick()
local debounceEnabled = getAttr(pad, "DebounceEnabled", DEFAULTS.DebounceEnabled)
local debounceTime = getAttr(pad, "DebounceTime", DEFAULTS.DebounceTime)
if debounceEnabled and (now - lastUse < debounceTime) then return end
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
local hrp = character and character:FindFirstChild("HumanoidRootPart")
if not (player and hrp) then return end
-- Only pads marked as HazardPad
if not pad:GetAttribute("HazardPad") then return end
lastUse = now
-- Fling logic
local flingEnabled = getAttr(pad, "FlingEnabled", DEFAULTS.FlingEnabled)
if flingEnabled then
local flingRandom = getAttr(pad, "FlingRandom", DEFAULTS.FlingRandom)
local flingPower = getAttr(pad, "FlingPower", DEFAULTS.FlingPower)
local flingVertical = getAttr(pad, "FlingVertical", DEFAULTS.FlingVertical)
if flingRandom then
local min = getAttr(pad, "FlingRandomMin", DEFAULTS.FlingRandomMin)
local max = getAttr(pad, "FlingRandomMax", DEFAULTS.FlingRandomMax)
flingPower = math.random(min, max)
end
local forwardVel = pad.CFrame.LookVector * flingPower
local upwardVel = Vector3.new(0, flingVertical, 0)
local finalVel = Vector3.new(forwardVel.X, upwardVel.Y, forwardVel.Z)
local bv = Instance.new("BodyVelocity")
bv.Velocity = finalVel
bv.MaxForce = Vector3.new(1e5, 1e5, 1e5)
bv.P = 1250
bv.Parent = hrp
Debris:AddItem(bv, 0.5)
end
-- Play sound if present
local soundName = getAttr(pad, "SoundTemplateName", DEFAULTS.SoundTemplateName)
local soundTemplate = script:FindFirstChild(soundName)
if soundTemplate and soundTemplate:IsA("Sound") then
local sound = soundTemplate:Clone()
sound.Parent = pad
sound:Play()
Debris:AddItem(sound, sound.TimeLength)
end
-- Rotate pad if desired
local rotationAngle = getAttr(pad, "RotationAngle", DEFAULTS.RotationAngle)
local rotateTime = getAttr(pad, "RotateTime", DEFAULTS.RotateTime)
local restoreTime = getAttr(pad, "RestoreTime", DEFAULTS.RestoreTime)
if rotationAngle ~= 0 then
local originalCF = pad.CFrame
local pitchCF = CFrame.Angles(math.rad(-rotationAngle), 0, 0)
local targetCF = originalCF * pitchCF
local rotateTween = TweenService:Create(pad, TweenInfo.new(rotateTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
CFrame = targetCF
})
local restoreTween = TweenService:Create(pad, TweenInfo.new(restoreTime, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out), {
CFrame = originalCF
})
rotateTween:Play()
rotateTween.Completed:Wait()
restoreTween:Play()
end
-- Damage logic
local damageEnabled = getAttr(pad, "DamageEnabled", DEFAULTS.DamageEnabled)
if damageEnabled then
local damageType = string.lower(getAttr(pad, "DamageType", DEFAULTS.DamageType))
local damageAmount = getAttr(pad, "DamageAmount", DEFAULTS.DamageAmount)
applyDamage(character, damageAmount, damageType)
end
-- Stun logic
local stunEnabled = getAttr(pad, "StunEnabled", DEFAULTS.StunEnabled)
if stunEnabled then
local stunType = string.lower(getAttr(pad, "StunType", DEFAULTS.StunType))
local stunTime = getAttr(pad, "StunTime", DEFAULTS.StunTime)
if stunType == "light" or stunType == "heavy" then
applyStun(character, stunType, stunTime)
end
end
end)
end
-- Initialize existing pads
local hazardPadsFolder = script.Parent
for _, pad in ipairs(hazardPadsFolder:GetDescendants()) do
if pad:IsA("BasePart") and pad:GetAttribute("HazardPad") then
setupPad(pad)
end
end
-- Handle new pads being added at runtime
hazardPadsFolder.DescendantAdded:Connect(function(desc)
if desc:IsA("BasePart") and desc:GetAttribute("HazardPad") then
setupPad(desc)
end
end)
--[[
=== ATTRIBUTE REFERENCE ===
Set these attributes on each pad (BasePart) as needed:
- HazardPad (bool) -- Must be true for pad to be active
- FlingEnabled (bool)
- FlingPower (number)
- FlingVertical (number)
- FlingRandom (bool)
- FlingRandomMin (number)
- FlingRandomMax (number)
- RotationAngle (number)
- RotateTime (number)
- RestoreTime (number)
- DebounceEnabled (bool)
- DebounceTime (number)
- DamageEnabled (bool)
- DamageType (string) -- "fire", "ice", "poison", "none"
- DamageAmount (number)
- StunEnabled (bool)
- StunType (string) -- "none", "light", "heavy"
- StunTime (number)
- SoundTemplateName (string) -- Name of Sound under this script
Pads with no attribute set will use the default values above.
]]
remove the rotation on touch stuff and make the code actually work.