I'm trying to make a gui pop up when the player gets near a npc. I've managed to come up with a local script under StarterPlayerScripts that has this:


local RegionDetector = require(script.RegionDetector)

local NpcRange = RegionDetector:NewRegion(workspace.Tim.HumanoidRootPart.Position, 20)

function NpcRange.EnteredRegion()
	print("Player has entered the region")
end

function NpcRange.LeftRegion()
	print("Player has left the region")
end


and under this I have a module script that goes:

local PlayerService = game:GetService("Players")

local RegionDetector = {}

function RegionDetector:NonRegion(origin, radius)
	local regionPart = Instance.new("Part")
	regionPart.CanCollide = false
	regionPart.Anchored = true
	regionPart.Shape = Enum.PartType.Ball
	regionPart.Transparency = 1
	regionPart.Size = Vector3.new(radius, radius, radius)^2
	regionPart.CFrame = CFrame.new(origin)
	regionPart.Parent = workspace
	
	local regionEvents = {EnteredRegion = nil, LeftRegion = nil}
	local playerInRegion = false
	
	regionPart.Touched:Connect(function(part)
		if PlayerService:GetPlayerFromCharacter(part.Parent) and part.Name == "HumanoidRootPart" and not playerInRegion then
			playerInRegion = true
			if regionEvents.EnteredRegion then
				regionEvents.EnteredRegion()
				
			end
		end
end)

regionPart.TouchedEnded:Connect(function(part)
			if PlayerService:GetPlayerFromCharacter(part.Parent) and part.Name == "HumanoidRootPart" and  playerInRegion then
			playerInRegion = false
			if regionEvents.LeftRegion then
				regionEvents.LeftRegion()
	end
	end
	
end)
return regionEvents
end

return RegionDetector


Roblox found no errors but when I check the output I get:

  01:19:12.278 - Players.Darksniper600.PlayerScripts.LocalScript:4: attempt to call a nil value

I'm trying to print the outputs so I can add the gui there also i'm not sure why the error is coming from the local script if the nil value is in the module script

2 comments

1

You defined a method called NonRegion, not NewRegion. For the error occurring in the script, compilers add whatever code is within the file that was imported into the code of the program.

0

thanks!