Region3 not working as expected, help?

Well, basically i want my region to collect coins, but there’s an issue.
Sometimes, it refuses to collect them. I’m pretty unsure as to why, as i learned how to use region’s litteraly 3 minutes ago, any help is appreciated!

local holding = false
local tweenservice = game:GetService("TweenService")
script.Parent.Equipped:Connect(function()
	holding = true
	local HRP = script.Parent.Parent.HumanoidRootPart
	local part = Instance.new("Part")
	part.Size = Vector3.new(60,60,60)
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0.8
	part.Parent = workspace
	while holding do
		task.wait()
		local region = Region3.new(script.Parent.Handle.Position + Vector3.new(-30,-30,-30),script.Parent.Handle.Position + Vector3.new(30,30,30))
		part.CFrame = region.CFrame
		local coins = workspace:FindPartsInRegion3(region,nil)
		for _,Coin in pairs(coins) do
			if Coin.Name == "Coin" then
				if Coin.Picked.Value == false then
					task.spawn(function()
						Coin.Picked.Value = true
						Coin.Owner.Value = script.Parent.Parent.Name
						local beam1,beam2 = game.ReplicatedStorage.beams.Basic:Clone(), game.ReplicatedStorage.beams.Basic:Clone()
						beam1.Parent = script.Parent.Handle.Tips.beam1
						beam1.Attachment0 = script.Parent.Handle.Tips.beam1
						beam1.Attachment1 = Coin.beamAttachment
						beam2.Parent = script.Parent.Handle.Tips.beam2
						beam2.Attachment0 = script.Parent.Handle.Tips.beam2
						beam2.Attachment1 = Coin.beamAttachment

						for i = 1,10,1 do
							tween = tweenservice:Create(Coin,TweenInfo.new(0.75,Enum.EasingStyle.Exponential),{
								Orientation = HRP.Orientation,
								Position = script.Parent.Handle.Collect.WorldPosition,
								Transparency = 1
							})
							task.wait()
							tween:Play()
						end
						Coin:Destroy()
					end)
				end
			end
		end
		region = nil
	end
end)

script.Parent.Unequipped:Connect(function()
	holding = false
end)

Maybe try using OverlapParams instead of Region3, Region3 is deprecated in favor of OverlapParams so it might help.

1 Like

Thank you! I set it up so easily, and it works like a charm. < 3

1 Like
local ts = game:GetService("TweenService")
local plrs = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")

local beams = rs:WaitForChild("beams")
local basic = beams:WaitForChild("Basic")

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local collect = handle:WaitForChild("Collect")
local tips = handle:WaitForChild("Tips")
local beam1 = tips:WaitForChild("beam1")
local beam2 = tips:WaitForChild("beam2")

local holding = false
local debounce = false

tool.Equipped:Connect(function()
	if debounce then
		return
	end
	debounce = true
	
	holding = true
	local plr = plrs:GetPlayerFromCharacter(tool.Parent)
	local char = plr.Character
	local hrp = char:WaitForChild("HumanoidRootPart")
	
	local part = Instance.new("Part")
	part.Size = Vector3.new(60, 60, 60)
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0.8
	part.Parent = workspace
	
	while task.wait() do
		if holding then
			local region = Region3.new(handle.Position - Vector3.new(30, 30, 30), handle.Position + Vector3.new(30, 30, 30))
			part.CFrame = handle.CFrame
			local coins = workspace:FindPartsInRegion3(region, nil)
			for _, coin in ipairs(coins) do
				if coin.Name == "Coin" then
					if not coin.Picked.Value then
						coin.Picked.Value = true
						coin.Owner.Value = plr.Name
						local beam1 = basic:Clone()
						beam1.Parent = beam1
						beam1.Attachment0 = beam1
						beam1.Attachment1 = coin:WaitForChild("beamAttachment")
						local beam2 = basic:Clone()
						beam2.Parent = beam2
						beam2.Attachment0 = beam2
						beam2.Attachment1 = coin:WaitForChild("beamAttachment")
						task.spawn(function()
							local tweenInfo = TweenInfo.new(0.75,Enum.EasingStyle.Exponential, 1)
							local tweenGoals = {Orientation = hrp.Orientation, Position = collect.WorldPosition, Transparency = 1}
							local tween = ts:Create(coin, tweenInfo, tweenGoals)
							tween:Play()
							tween.Completed:Wait()
							tween.C:Destroy()
						end)
					end
				end
			end
		end
	end
	task.wait(10)
	debounce = false
end)

tool.Unequipped:Connect(function()
	holding = false
end)

I know this is solved but I wanted to fix the original script.

you didn’t really fix it, you just made it longer lol, i’ve already fixed it using the overlapparams

It’s longer but a lot cleaner, with the necessary fixes, the coin tweening was awkward because I wasn’t sure what exactly you were trying to do, it seemed as if you were trying to create 10 tweens per coin and play them all even though the tweens were exactly the same.

because if the player is moving it’ll tween behind the player, it basically updates the tween really fast.
Also, you aren’t supposed to have a debounce on it.