I am trying to make a simple radiation script which detects if a player is close to one of the bricks inside a folder. If the player is close enough it makes the dosimeter transparent and it plays a clicking sound for now. If you are not close enough to a brick it will print (“stop”) until you are close enough. The problem I have is, that if you come close enough, it will keep printing stop, because you are “not close enough to the other bricks”. This causes the clicking sound to keep starting and ending itself until you leave the area. One weird thing is, that one of the three bricks will work correctly, and stop printing(“stop”), the others don’t. This means, one of the brick works as usual. I hope somebody has an idea how to fix this. Code and video is below
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local detect = false
while rs.Stepped:Wait() do
for i, v in pairs(workspace.Radiation:GetChildren()) do
if v.ClassName == "Part" then
local humrp = player.Character:WaitForChild("HumanoidRootPart")
if humrp then
local pos = humrp.Position - v.Position
local Magnitude = pos.magnitude
if Magnitude <= 20 then
local detect = true
print(detect)
else
detect = false
print(detect)
end
end
end
end
end
end)
end)
[Chernobyl test - Roblox Studio (gyazo.com)](https://gyazo.com/9ef77e3ff81468c5e9d72c4224d6661c)
This is a reupload because I did not get any help.
You can add a variable to check if player is already close to a brick
local CLOSE = false
-- before playing the sound do this --
if not CLOSE then
-- your code --
CLOSE = true
-- also when not in range with any of the bricks set it to false again --
I am having issues setting up the code you said, what do I put on the else part?
local rs = game:GetService("RunService")
local CLOSE = false
game.Players.PlayerAdded:Connect(function(player)--The player that joined
player.CharacterAdded:Connect(function(char) --The player character is loaded
while rs.Stepped:Wait() do
for i, v in pairs(workspace.Radiation:GetChildren()) do --This is a model of parts that u want to detect if the player is near it. NOTE:Change the location to your folder that has parts inside it!!
if v.ClassName == "Part" then
local humrp = player.Character:WaitForChild("HumanoidRootPart")--Player humanoid root part
if humrp then
local pos = humrp.Position - v.Position--Getting the part and humanoidrootpart Position
local Magnitude = pos.magnitude --Getting the Magnitude
if Magnitude <= 20 then --Add any number you want
if player.Character:FindFirstChild("Dosimeter") then
player.Character.Dosimeter.Transparency = 0
if not CLOSE then
if player.Character.Dosimeter.clicking.Playing == false then
player.Character.Dosimeter.clicking:Play()
end
CLOSE = true
print(Magnitude)
print("radiation near")
end
else
if CLOSE == ? ?
if player.Character:FindFirstChild("Dosimeter") then
player.Character.Dosimeter.Transparency = 1
player.Character.Dosimeter.clicking:Stop()
end
end
end
end
end
end end
end)
end)``` I noticed that I have posted the wrong code btw, this is what I am stuck on.
local rs = game:GetService("RunService")
local CLOSE = false
game.Players.PlayerAdded:Connect(function(player)--The player that joined
player.CharacterAdded:Connect(function(char) --The player character is loaded
while rs.Stepped:Wait() do
for i, v in pairs(workspace.Radiation:GetChildren()) do --This is a model of parts that u want to detect if the player is near it. NOTE:Change the location to your folder that has parts inside it!!
if v.ClassName == "Part" then
local humrp = player.Character:WaitForChild("HumanoidRootPart")--Player humanoid root part
if humrp then
local pos = humrp.Position - v.Position--Getting the part and humanoidrootpart Position
local Magnitude = pos.magnitude --Getting the Magnitude
if Magnitude <= 20 then --Add any number you want
if not CLOSE then
CLOSE = true
if player.Character:FindFirstChild("Dosimeter") then
player.Character.Dosimeter.Transparency = 0
if player.Character.Dosimeter.clicking.Playing == false then
player.Character.Dosimeter.clicking:Play()
end
print(Magnitude)
print("radiation near")
end
end
else
CLOSE = false
if player.Character:FindFirstChild("Dosimeter") then
player.Character.Dosimeter.Transparency = 1
player.Character.Dosimeter.clicking:Stop()
end
end
end
end
end
end
end)
end)
llocal rs = game:GetService("RunService")
local MAX_DISTANCE = 20
local radioactiveParts = workspace:WaitForChild("Radiation"):GetChildren()
local function GetClosestPart(position: Vector3): BasePart?
local min = math.huge
local closestPart
for i = 1,#radioactiveParts do
local magnitude = (position - radioactiveParts[i].Position).Magnitude
if magnitude < MAX_DISTANCE then
if magnitude < min then
min = magnitude
closestPart = radioactiveParts[i]
end
end
end
return closestPart,min
end
game.Players.PlayerAdded:Connect(function(player)--The player that joined
player.CharacterAdded:Connect(function(char) --The player character is loaded
rs.Stepped:Connect(function()
local part,min = GetClosestPart(char.HumanoidRootPart.Position)
if part then
print('Part nearby')
print(min)
--Code here
else
print('No parts nearby')
end
end)
end)
end)