Trouble with Randomly generated obstacles

Hello! I am having trouble with randomly generating obstacles. I want it so that there is a chance that a light in a room will turn blue and if you walk under it it will kill you. This is in randomly generated rooms, and it seems that if one room has the light turn blue, they all do, instead of them all having a different outcome. Code:

local lightpart = script.Parent.LightPart
local area = script.Parent.DangerArea
local cursed = math.random(1,2)
local danger = false
if cursed == 2 then
	lightpart.BrickColor = BrickColor.Blue()
	danger = true
end

area.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and danger == true then
		hit.Parent.Humanoid.Health = 0
	end
end)

if danger == not true then
area:Destroy()
end

(50% chance for testing purposes)
I don’t know how to fix it, so any help would be appreciated! :slight_smile:

Does anything show up in the output?

Yeah it just spams this:

Is DangerArea inside the model? The error is self-explanatory.

Yes it is.
image
The reason that this is erroring is because I deleted the part from the clone (I presume) and idk why it’s not different for each room.

if cursed == 2 then
	lightpart.BrickColor = BrickColor.Blue()
	danger = true
else
   print("Cursed = "..Cursed
end

Try the following

I get this:
image

Is the DangerArea anchored? The part might be getting destroyed which is why it says it’s not in the model.

1 Like
area.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and danger == true then
		hit.Parent.Humanoid.Health = 0
      elseif danger == false then
        area:Destroy()
       end
	end
end)
1 Like

Oh I just removed a wait from a server script and it fixed it’s self. I still don’t know what the error was, but it’s working now. Thanks

1 Like

Oh it’s erroring as soon as I change the probability of the light to turn blue. This makes no sense

edit: It errors if cursed = something other than 2 on the first random number

edit2: I came up with a strange solution of moving the kill part underground instead of deleting it. Script:

if danger == not true then
area.CFrame = CFrame.new(area.CFrame.X, area.CFrame.Y - 100, area.CFrame.Z)
end