Magnitude Detecting one model only

Hi, so I am trying to make a detector where the local script detects how close the player is to the eggs in the folder

So the issue is that the detector is suppose to detect how close the player character is to the all the eggs in the folder but instead it only detects one of the eggs from the folder.

Is there anyway I could fix this issue?

Please post your code and an image of the explorer where the eggs are.

1 Like
RunService.Heartbeat:Connect(function()
for i, v in pairs(workspace.Eggs:GetChildren())do
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude <= 8 then
		if deb == false then deb = true
				local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
				frame["canActivate"].Value = true
				frame["EggName"].Value = v.Name
				TweenModule.TweenFrame(frame, {Position = UDim2.new(0.5, 0,0.5, 0)}, 0.25)
				end	
else
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude > 8 then
				
	local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
    frame["canActivate"].Value = false     
    TweenModule.TweenFrame(frame, {Position = UDim2.new(0.5,0,1.5,0)}, 0.25)
    deb = false
	end
	end
	end end)

image

The reason this isn’t working is because when you set your debounce to true here:

if deb == false then deb = true

you are not setting it back to false, Not allowing this to be ran again:

local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
		frame["canActivate"].Value = true
		frame["EggName"].Value = v.Name
	TweenModule.TweenFrame(frame, {Position = UDim2.new(0.5, 0,0.5, 0)}, 0.25)

So to fix this simply set your debounce back to false once the tween is over

Also if i may ask, is there are reason you are using heartbeat for this?

1 Like

The issue is that the detetector only detects the uncommon egg and not the common egg.

The reason I am using heartbeat is so that I can detect how far the player’s character is from the eggs in the folder

In this case, rather than using a bool for debounce, use a table.

First, declare an empty table: local deb = {}

Change “deb = true” to “deb[v] = true”, and check debounce with “if not deb[v] then

This way it will save a debounce for each specific egg.

now the script doesn’t detector any eggs in the folder

RunService.Heartbeat:Connect(function()
for i, v in pairs(workspace.Eggs:GetChildren())do
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude <= 8 then
		if not deb[v] then deb[v] = true
				local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
				frame["canActivate"].Value = true
				frame["EggName"].Value = v.Name
				end	
else
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude > 8 then		
	local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
    frame["canActivate"].Value = false     
    deb[v] = false
	end
	end
	end
end)

Oops, my bad.

Instead of using ‘if not deb[v]’ use ‘if deb[v] == nil or deb[v] == false then’

didnt change anything

RunService.Heartbeat:Connect(function()
for i, v in pairs(workspace.Eggs:GetChildren())do
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude <= 8 then
		if deb[v] == nil or deb[v] == false then deb[v] = true
				local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
				frame["canActivate"].Value = true
				frame["EggName"].Value = v.Name
				end	
else
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude > 8 then		
	local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
    frame["canActivate"].Value = false     
    deb[v] = false
	end
	end
	end
end)

i changed/ re-wrote your script, try this instead:

local Debounces = {}

while true do
	wait(0.1)

for i, v in pairs(workspace.Eggs:GetChildren())do
	
	if Debounces[v] == nil then
		Debounces[v]  = false
	end

	if  Debounces[v] == false and (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude <= 8 then
    Debounces[v] = true
	local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
	frame["canActivate"].Value = true
	frame["EggName"].Value = v.Name
	TweenModule.TweenFrame(frame, {Position = UDim2.new(0.5, 0,0.5, 0)}, 0.25)
 else
	if (v.PrimaryPart.Position - player.Character.HumanoidRootPart.Position).Magnitude > 8 then	
    local frame = player["PlayerGui"]["onTouchGui"]["Selections"]["EggOpenFrame"]
    frame["canActivate"].Value = false     
     TweenModule.TweenFrame(frame, {Position = UDim2.new(0.5,0,1.5,0)}, 0.25)
    Debounces[v] = false
	end
    end
  end
end
3 Likes

and now canActivate is true for like a second then goes false before the frame could even pop up.

EDIT: ended up finding the fix, thanks to everyone who contributed

1 Like

Theres an alternative method. Using CollectionService is a lot easier. Well basically adding a tag to each egg then in a script:

CollectionService:GetTagged("YourTagHere")

Then do a for loop for the tag

for i,v in pairs(YourTagVariableHere) do
       --Do your thing
       wait()
end