this code.
--[[
HazardScript.lua
Attribute-driven hazard pad script for Roblox.
Supports: Fling (with XYZ vector), 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.
Fling is now based on XYZ attributes, not pad orientation.
Enhanced: Custom death effects for fire/ice, improved debounce, pad cannot activate if player is dead.
Enhanced: Multiple template sounds, choose play location (pad/player).
Debounce: Per-player cooldown before pad can activate for that player again.
Enhanced: Play sound on player death, with play location option.
Sound always plays from the hit player (HumanoidRootPart if available, else character).
]]
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
-- Default configuration (used if attribute is missing)
local DEFAULTS = {
FlingEnabled = true,
FlingX = 0,
FlingY = 0,
FlingZ = 0,
FlingRandom = false,
FlingRandomMinX = 0,
FlingRandomMaxX = 0,
FlingRandomMinY = 0,
FlingRandomMaxY = 0,
FlingRandomMinZ = 0,
FlingRandomMaxZ = 0,
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,
SoundTemplateNames = "sfx", -- Comma-separated list of sound template names
DeathSoundTemplateNames = "" -- Comma-separated list of death sound template names
}
-- 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
-- Make all parts/accessories/etc completely black
local function makeCharacterCompletelyBlack(character)
for _, item in ipairs(character:GetDescendants()) do
if item:IsA("BasePart") or item:IsA("MeshPart") then
item.Color = Color3.new(0, 0, 0)
item.Material = Enum.Material.SmoothPlastic
elseif item:IsA("Accessory") then
local handle = item:FindFirstChild("Handle")
if handle and handle:IsA("BasePart") then
handle.Color = Color3.new(0, 0, 0)
handle.Material = Enum.Material.SmoothPlastic
local mesh = handle:FindFirstChildWhichIsA("SpecialMesh")
if mesh then
mesh.VertexColor = Vector3.new(0, 0, 0)
end
end
elseif item:IsA("Decal") or item:IsA("Shirt") or item:IsA("Pants") or item:IsA("ShirtGraphic") or item:IsA("CharacterMesh") then
item:Destroy()
end
end
end
-- Helper: Play one or more template sounds, always from the hit player (HRP if available, else character)
local function playTemplateSoundFromPlayer(templateNamesAttr, character)
if not templateNamesAttr or templateNamesAttr == "" then return end
local soundNames = {}
for name in string.gmatch(templateNamesAttr, "([^,]+)") do
table.insert(soundNames, name:match("^%s*(.-)%s*$"))
end
if #soundNames == 0 then return end
local chosenName = soundNames[math.random(1, #soundNames)]
local soundTemplate = script:FindFirstChild(chosenName)
if soundTemplate and soundTemplate:IsA("Sound") then
local sound = soundTemplate:Clone()
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
sound.Parent = hrp
else
sound.Parent = character
end
sound:Play()
Debris:AddItem(sound, sound.TimeLength)
end
end
-- Encase the player in ice (death effect)
local function encaseInIce(player)
local character = player.Character
if not character or not character:FindFirstChild("HumanoidRootPart") then return end
local hrp = character.HumanoidRootPart
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if not humanoid then return end
-- Set BreakJointsOnDeath to false BEFORE killing
humanoid.BreakJointsOnDeath = false
-- Make them completely black
makeCharacterCompletelyBlack(character)
-- Freeze player
humanoid.PlatformStand = true
-- Ice block sizing
local modelCFrame, modelSize = character:GetBoundingBox()
local iceSize = modelSize + Vector3.new(3, 1.5, 3)
local iceBlock = Instance.new("Part")
iceBlock.Name = "IceEncasement"
iceBlock.Size = iceSize
iceBlock.CFrame = modelCFrame
iceBlock.Material = Enum.Material.Ice
iceBlock.Transparency = 0.3
iceBlock.BrickColor = BrickColor.new("Bright blue")
iceBlock.CanCollide = true
iceBlock.Anchored = false
iceBlock.Parent = character
-- Weld ice to player
local weld = Instance.new("WeldConstraint")
weld.Part0 = hrp
weld.Part1 = iceBlock
weld.Parent = iceBlock
-- Play sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://6092902279"
sound.Volume = 5
sound.Parent = hrp
sound:Play()
sound.Ended:Connect(function()
sound:Destroy()
end)
-- Kill the player
humanoid:TakeDamage(9999)
humanoid.Health = 0
humanoid.MaxHealth = 0
end
-- Custom death effect for fire (turns player black)
local function fireDeathEffect(player)
local character = player.Character
if not character then return end
makeCharacterCompletelyBlack(character)
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(9999)
humanoid.Health = 0
humanoid.MaxHealth = 0
end
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
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
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"
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
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"
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
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp then
hrp.Anchored = true
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
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
-- Debounce table for per-player per-pad cooldown
local padDebounce = {}
-- Main pad setup
local function setupPad(pad)
padDebounce[pad] = padDebounce[pad] or {}
pad.Touched:Connect(function(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
local hrp = character and character:FindFirstChild("HumanoidRootPart")
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if not (player and hrp and humanoid) then return end
-- Don't activate if player is dead
if humanoid.Health <= 0 then return end
if not pad:GetAttribute("HazardPad") then return end
-- Per-player debounce
local debounceEnabled = getAttr(pad, "DebounceEnabled", DEFAULTS.DebounceEnabled)
local debounceTime = getAttr(pad, "DebounceTime", DEFAULTS.DebounceTime)
local now = tick()
local lastUse = padDebounce[pad][player] or 0
if debounceEnabled and (now - lastUse < debounceTime) then return end
padDebounce[pad][player] = now
-- Fling logic (XYZ)
local flingEnabled = getAttr(pad, "FlingEnabled", DEFAULTS.FlingEnabled)
if flingEnabled then
local flingRandom = getAttr(pad, "FlingRandom", DEFAULTS.FlingRandom)
local x, y, z
if flingRandom then
local minX = getAttr(pad, "FlingRandomMinX", DEFAULTS.FlingRandomMinX)
local maxX = getAttr(pad, "FlingRandomMaxX", DEFAULTS.FlingRandomMaxX)
local minY = getAttr(pad, "FlingRandomMinY", DEFAULTS.FlingRandomMinY)
local maxY = getAttr(pad, "FlingRandomMaxY", DEFAULTS.FlingRandomMaxY)
local minZ = getAttr(pad, "FlingRandomMinZ", DEFAULTS.FlingRandomMinZ)
local maxZ = getAttr(pad, "FlingRandomMaxZ", DEFAULTS.FlingRandomMaxZ)
x = math.random(minX, maxX)
y = math.random(minY, maxY)
z = math.random(minZ, maxZ)
else
x = getAttr(pad, "FlingX", DEFAULTS.FlingX)
y = getAttr(pad, "FlingY", DEFAULTS.FlingY)
z = getAttr(pad, "FlingZ", DEFAULTS.FlingZ)
end
local finalVel = Vector3.new(x, y, 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(s) if present (always from the hit player)
local soundTemplateNames = getAttr(pad, "SoundTemplateNames", DEFAULTS.SoundTemplateNames)
playTemplateSoundFromPlayer(soundTemplateNames, character)
-- 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)
-- Custom death effects for fire/ice
if humanoid.Health - damageAmount <= 0 then
-- Play death sound if configured (always from the hit player)
local deathSoundTemplateNames = getAttr(pad, "DeathSoundTemplateNames", DEFAULTS.DeathSoundTemplateNames)
playTemplateSoundFromPlayer(deathSoundTemplateNames, character)
if damageType == "fire" then
fireDeathEffect(player)
elseif damageType == "ice" then
encaseInIce(player)
end
end
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)
- FlingX (number)
- FlingY (number)
- FlingZ (number)
- FlingRandom (bool)
- FlingRandomMinX (number)
- FlingRandomMaxX (number)
- FlingRandomMinY (number)
- FlingRandomMaxY (number)
- FlingRandomMinZ (number)
- FlingRandomMaxZ (number)
- DebounceEnabled (bool)
- DebounceTime (number) -- Per-player cooldown time
- DamageEnabled (bool)
- DamageType (string) -- "fire", "ice", "poison", "none"
- DamageAmount (number)
- StunEnabled (bool)
- StunType (string) -- "none", "light", "heavy"
- StunTime (number)
- SoundTemplateNames (string) -- Comma-separated list of Sound names under this script
- DeathSoundTemplateNames (string) -- Comma-separated list of Sound names under this script for death
Pads with no attribute set will use the default values above.
]]
the deboucne is supposed to add in a cooldown for the touch pads. make it work like that. fix all the attributes. even the sounds are not playing fix it please.