Need help with radiation script

  1. What do you want to achieve?
    I want it so when the “Equiped” boolvalue inside of “Hazmat suit” is true, the player won’t take damage, and when it is false, the player will take damage.

  2. What is the issue?
    The issue is that it won’t work no matter what the “Equiped” value is. The player still takes damage when touching the “Rad” part.

Also yes, everything is located correctly including the “Equiped” value and “Hazmat suit.”. Btw the “Hazmat suit” script handles weather the “Equiped” value = true or not.

  1. What solutions have you tried so far?
    I’ve tried asking ChatGPT multiple times about my issue, but it can’t seem to fix it.

This is the script that I am having issues with:

local Players = game:GetService("Players")
local RadiationZones = workspace:WaitForChild("RadiationZones")

local playersInRadiation = {}

local function startRadiationEffect(player, part)
    local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
    local level = part:FindFirstChild("Level")
    if humanoid and level and level:IsA("NumberValue") then
        -- Look for the Hazmat suit dynamically in the character's hierarchy
        local character = player.Character
        local hazmatSuit = character and character:FindFirstChild("Hazmat suit")
        if hazmatSuit then
            local equipValue = hazmatSuit:FindFirstChild("Equiped")
            if equipValue and equipValue:IsA("BoolValue") then
                if not equipValue.Value then
                    local healthReducer = coroutine.create(function()
                        while playersInRadiation[player] == part and humanoid.Health > 0 do
                            humanoid.Health = humanoid.Health - level.Value
                            wait(1)
                        end
                    end)
                    coroutine.resume(healthReducer)
                else
                    print("TRUE - No damage taken")
                    return
                end
            else
                print("Equiped BoolValue not found or not a BoolValue in Hazmat suit")
            end
        else
            print("Hazmat suit not found in character")
        end
    end
end

local function connectTouchEvents(part)
    part.Touched:Connect(function(hit)
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player and not playersInRadiation[player] then
            playersInRadiation[player] = part

            -- Check Hazmat suit when player touches Rad part
            local hazmatSuit = character:FindFirstChild("Hazmat suit")
            if hazmatSuit then
                local equipValue = hazmatSuit:FindFirstChild("Equiped")
                if equipValue and equipValue:IsA("BoolValue") then
                    startRadiationEffect(player, part)
                else
                    print("Equiped BoolValue not found or not a BoolValue in Hazmat suit")
                end
            else
                print("Hazmat suit not found in character")
            end
        end
    end)

    part.TouchEnded:Connect(function(hit)
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player and playersInRadiation[player] == part then
            playersInRadiation[player] = nil
        end
    end)
end

-- Function to connect touch events for existing Rad parts
local function connectExistingRadParts()
    for _, radPart in pairs(RadiationZones:GetChildren()) do
        if radPart:IsA("BasePart") and radPart.Name == "Rad" then
            connectTouchEvents(radPart)
        end
    end
end

-- Connect touch events for existing Rad parts
connectExistingRadParts()

-- Connect new Rad parts added to the RadiationZones folder
RadiationZones.ChildAdded:Connect(function(child)
    if child:IsA("BasePart") and child.Name == "Rad" then
        connectTouchEvents(child)
    end
end)

-- Handle players joining the game
Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- Wait for the character to fully load
        wait(1)
        local hazmatSuit = character:FindFirstChild("Hazmat suit")
        if hazmatSuit then
            connectExistingRadParts()
        end
    end)
end)

-- Ensure playersInRadiation is updated properly when players leave the game
Players.PlayerRemoving:Connect(function(player)
    playersInRadiation[player] = nil
end)

And for context here is the original script without checking the “Equiped” value. (This works fine, but I want it like I explained)

local RadiationZones = game.Workspace:WaitForChild("RadiationZones")
local Players = game:GetService("Players")



-- Table to keep track of players in radiation zones
local playersInRadiation = {}

-- Function to handle touching Rad parts
local function startRadiationEffect(player, part)
	local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
	local level = part:FindFirstChild("Level")
	if humanoid and level and level:IsA("NumberValue") then
		local healthReducer = coroutine.create(function()
			while playersInRadiation[player] == part and humanoid.Health > 0 do
				humanoid.Health = humanoid.Health - level.Value
				wait(1)
			end
		end)
		coroutine.resume(healthReducer)
	end
end

-- Function to connect touch events
local function connectTouchEvents(part)
	part.Touched:Connect(function(hit)
		local character = hit.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player and not playersInRadiation[player] then
			playersInRadiation[player] = part
			startRadiationEffect(player, part)
		end
	end)

	part.TouchEnded:Connect(function(hit)
		local character = hit.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player and playersInRadiation[player] == part then
			playersInRadiation[player] = nil
		end
	end)
end

-- Connect touch events for each Rad part
for _, radPart in pairs(RadiationZones:GetChildren()) do
	if radPart:IsA("BasePart") and radPart.Name == "Rad" then
		connectTouchEvents(radPart)
	end
end

-- Connect new Rad parts added to the RadiationZones folder
RadiationZones.ChildAdded:Connect(function(child)
	if child:IsA("BasePart") and child.Name == "Rad" then
		connectTouchEvents(child)
	end
end)

-- Ensure playersInRadiation is updated properly when players leave the game
Players.PlayerRemoving:Connect(function(player)
	playersInRadiation[player] = nil
end)

If anyone can help me fix this or point out what I did wrong it would be much appreciated.

local Players = game:GetService("Players")
local RadiationZones = workspace:WaitForChild("RadiationZones")

local playersInRadiation = {}

local function startRadiationEffect(player, part)
    local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
    local level = part:FindFirstChild("Level")
    if humanoid and level and level:IsA("NumberValue") then
        local character = player.Character
        local hazmatSuit = character and character:FindFirstChild("Hazmat suit")
        if hazmatSuit then
            local equipValue = hazmatSuit:FindFirstChild("Equiped")
            if equipValue and equipValue:IsA("BoolValue") then
                if not equipValue.Value then
                    print("Player " .. player.Name .. " is taking damage.")
                    local healthReducer = coroutine.create(function()
                        while playersInRadiation[player] == part and humanoid.Health > 0 do
                            humanoid.Health = humanoid.Health - level.Value
                            print("Player " .. player.Name .. " health reduced to " .. humanoid.Health)
                            wait(1)
                        end
                    end)
                    coroutine.resume(healthReducer)
                else
                    print("Player " .. player.Name .. " is protected by Hazmat suit.")
                end
            else
                print("Equiped BoolValue not found or not a BoolValue in Hazmat suit")
            end
        else
            print("Hazmat suit not found in character")
        end
    else
        print("Humanoid or Level not found in part")
    end
end

local function connectTouchEvents(part)
    part.Touched:Connect(function(hit)
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player and not playersInRadiation[player] then
            playersInRadiation[player] = part
            print("Player " .. player.Name .. " entered radiation zone")

            -- Check Hazmat suit when player touches Rad part
            local hazmatSuit = character:FindFirstChild("Hazmat suit")
            if hazmatSuit then
                local equipValue = hazmatSuit:FindFirstChild("Equiped")
                if equipValue and equipValue:IsA("BoolValue") then
                    startRadiationEffect(player, part)
                else
                    print("Equiped BoolValue not found or not a BoolValue in Hazmat suit")
                end
            else
                print("Hazmat suit not found in character")
            end
        end
    end)

    part.TouchEnded:Connect(function(hit)
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if player and playersInRadiation[player] == part then
            playersInRadiation[player] = nil
            print("Player " .. player.Name .. " left radiation zone")
        end
    end)
end

local function connectExistingRadParts()
    for _, radPart in pairs(RadiationZones:GetChildren()) do
        if radPart:IsA("BasePart") and radPart.Name == "Rad" then
            connectTouchEvents(radPart)
        end
    end
end

connectExistingRadParts()

RadiationZones.ChildAdded:Connect(function(child)
    if child:IsA("BasePart") and child.Name == "Rad" then
        connectTouchEvents(child)
    end
end)

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1)
        local hazmatSuit = character:FindFirstChild("Hazmat suit")
        if hazmatSuit then
            connectExistingRadParts()
        end
    end)
end)

Players.PlayerRemoving:Connect(function(player)
    playersInRadiation[player] = nil
    print("Player " .. player.Name .. " left the game")
end)

The solution from @markjac should work for you, but as an extra bit of feedback, to help you create more readable code (it was extremely hard for me to follow along with what was going on here.

Watch this video:

For example i’m going to fix up some of your code here from @markjac’s solution.

local function startRadiationEffect(player, part)
	local character = player.Character
	if character == nil then return error("Character is nil") end
	local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
	local level = part:FindFirstChild("Level")

	local hazmatSuit = character:FindFirstChild("Hazmat suit")

	-- Checking the humanoids and levels, returning warnings early if they are nil
	if humanoid == nil then return warn("Humanoid is nil") end
	if level == nil then return warn("Level does not exist") end
	if level:IsA("NumberValue") == false then warn("Level should be a number value") end
	if hazmatSuit == nil then return warn("Hazmat Suit is nil") end


	local equipValue = hazmatSuit:FindFirstChild("Equiped")

	if equipValue == nil then return error("Equip Value is nil") end
	if equipValue:IsA("BoolValue") == false then return error("Equip Value must be a BoolValu") end
	if equipValue.Value == true then return end
	
	print("Player " .. player.Name .. " is taking damage.")
	local healthReducer = coroutine.create(function()
		while playersInRadiation[player] == part and humanoid.Health > 0 do
			humanoid.Health = humanoid.Health - level.Value
			print("Player " .. player.Name .. " health reduced to " .. humanoid.Health)
			wait(1)
		end
	end)
	coroutine.resume(healthReducer)
end

Notice how we get rid of every single if-else and make it a lot easier to see what we need. Any condition that isn’t “perfect” is immediately returned early with an error or a warning, then we have our code run below. It’s much easier to edit your code this way.

What exactly is the issue that you’re running into, I can’t help you unless I know the issue

Nevermind, I fixed it. Thanks for the help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.