Unable to cast Instance to Region3

Hi! I’m trying to make a Region3 script where if you stand in the region for a second you get a dollar. In the output it says Unable to Cast Instance to Region3. I’ve checked forum posts and articles and they aren’t helping. Here’s the script (credit to @Forummer for helping me with it)

local RegionPart = workspace:WaitForChild("RegionPart")
local region = script.Parent

while task.wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, RegionPart, 1000)
	for i, part in ipairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") then
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				local money = player:FindFirstChild("Money")
					money.Value += 1
				end
			end
		end
	end
1 Like

FindPartsInRegion3WithIgnoreList requires an Region3 as the primary argument and a table of parts for the second, but it is better to use GetPartsInPart since everything related to Region3 will be deprecated

local RegionPart = workspace:WaitForChild("RegionPart")
local region = script.Parent

local OverlapP = OverlapParams.new()
OverlapP.MaxParts = 1000
OverlapP.FilterDescendantsInstances = {RegionPart}
OverlapP.FilterType = Enum.RaycastFilterType.Whitelist

while task.wait(1) do
	local partsInRegion = workspace:GetPartsInPart(region, OverlapP)
	for i, part in ipairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") then
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				local money = player:FindFirstChild("Money")
				money.Value += 1
			end
		end
	end
end

About OverlapParams

With these new functions, the following “Region3” based functions are all deprecated, and should not be used in new work:

  • Workspace:FindPartsInRegion3
  • Workspace:FindPartsInRegion3WithIgnoreList
  • Workspace:FindPartsInRegion3WithWhiteList
  • Workspace:IsRegion3Empty
  • Workspace:IsRegion3EmptyWithIgnoreList

Introducing OverlapParams - New Spatial Query API

1 Like

Thanks for replying! It doesn’t work and I’m starting to think it’s a problem in the workspace. The script is in the regionpart and the regionpart is just a purple part. Thank you so much for linking OverlapParams at first I was confused by the script

1 Like

Oh, in that case you were using the wrong part

local RegionPart = workspace:WaitForChild("RegionPart")
local region = script.Parent

local OverlapP = OverlapParams.new()
OverlapP.MaxParts = 1000

while task.wait(1) do
	local partsInRegion = workspace:GetPartsInPart(RegionPart, OverlapP)
	for i, part in ipairs(partsInRegion) do
		local player = Players:GetPlayerFromCharacter(part.Parent)
		if player then
			local money = player:FindFirstChild("Money")
			money.Value += 1
		end
	end
end
1 Like
local Players = game:GetService("Players")
local ActualRegion = script.Parent
local RegionPart = workspace:WaitForChild("RegionPart")
local Region = Region3.new(ActualRegion.Position - ActualRegion.Size/2, ActualRegion.Position + ActualRegion.Size/2)

while task.wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, RegionPart, 1000)
	for i, part in ipairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") then
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				local money = player:FindFirstChild("Money")
				money.Value += 1
			end
		end
	end
end

You need to use a Region3 value for the Region3 class functions. Is the region the script’s parent? and then ignore descendants is retrieved from the part named “RegionPart”?

I feel like I’m giving you all the work and I’m sorry I’m trying aswell. The region is the scripts parent and I ran the script and it doesn’t work. This time the output says " Unable to cast value to Objects" on line 7 and I don’t know what value it’s talking about. I think the value might be the region and the object the region part.