Field Module - Using Raycasting to Create "Zones"

Hello, this is my first release to the public. I want criticism and feedback from this module! I know if is definitely not perfect, but I thought I would share it, as it might help some developers who need this kind of system, but don’t have much experience scripting. Felt like making this after helping someone with a “field” problem.

Credit to Stephen Leitnick for his event module.

Introducing: Field Module!

I recently saw a module somewhat similar to this that used rotated region 3s, but mine is different from that. I simply raycast down from the HumanoidRootPart to detect if the player is in a “Field” which is a table of parts.

In the top of the Field modulescript, you can edit settings. You can change the minimum height of the player, and you can change the frequency of checks.

Usage

Creating a new field

local FieldModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Field"));
local MyField = FieldModule.new(PartTable);

Starting/stopping a field

FieldModule:Start(); -- Will start checking fields.

FieldModule:Stop(); -- Will stop checking fields.

Connecting to player left/entered events

MyField.PlayerEntered:Connect(function()

end)

MyField.PlayerLeft:Connect(function()

end)

Also, if you’re requiring this from the server, you can have a player in your PlayerEntered and PlayerLeft event. As of now, if you use this on the client, the local Player is passed only. I recommend you use this on the client, and if you need to communicate to the server that you have entered or left a field, fire an event. If you do use this on the server, might wanna lower the frequency of checks.

Here’s a gif of it in action.

Here’s the code I did for that.

local Player = game.Players.LocalPlayer;
local PlayerGui = Player:WaitForChild("PlayerGui");
local test = PlayerGui:WaitForChild("test");

local FieldModule = require(game:GetService("ReplicatedStorage"):WaitForChild("Field"));

local lowerfield = FieldModule.new({workspace.lowerpart});

lowerfield.PlayerEntered:Connect(function()
	test.lowerpart.Visible = true;
end)

lowerfield.PlayerLeft:Connect(function()
	test.lowerpart.Visible = false;
end)

local upperfields = FieldModule.new({workspace.higherpart, workspace.higherpart2});

upperfields.PlayerEntered:Connect(function()
	test.higherpart.Visible = true;
end)

upperfields.PlayerLeft:Connect(function()
	test.higherpart.Visible = false;
end)

FieldModule:Start();

Get it here: https://www.roblox.com/library/4864635222/Field-Module

UPDATE LOG

Update 2

  • Added setting bool “LEAVE_FIELD_ON_DEATH”
    • If set to true, when the player dies, they will leave the field (if they are in one)
  • Added bool FieldModule.Initialized
    • Is true if the module has started the main loop

Update 1

  • Added Field:Destroy()
    • Called on a returned field class to end it.
  • Added bool Field.Enabled
    • Property of returned field class.
5 Likes

Nice job! Can’t access the photo though:

1 Like

Thanks, updated the GIF link.
30 character