Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Region3 applying to everybody as a LocalScript?

Hello.

I am currently experimenting with Region3, and I am trying to set an area to which if the player is in, the fog will be 500 and it checks if a value is true or false. This does work, however, it shows for everybody else that plays the game, as a LocalScript. I'm very new to this and I am unsure where I went wrong.

Here's my script:

local RegionPart = workspace.RegionPart
local Pos1 = RegionPart.Position - (RegionPart.Size / 2)
local Pos2 = RegionPart.Position + (RegionPart.Size / 2)

local Region = Region3.new(Pos1, Pos2)
RegionPart:Destroy()

local Debounce = false
local Lighting = game:GetService("Lighting")

while true do
	wait()
	local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
	for _, Part in pairs(PartsInRegion) do
		if Part.Parent:FindFirstChild("Humanoid") ~= nil then
			local Character = Part.Parent
			if not (Debounce) then
				Debounce = true
				if Character.IsOutside.Value == true then
					print("Character is outside, changes not applied")
				else
					if (Lighting.FogEnd == 120) then
						Lighting.FogEnd = 500
					end
				end
				wait(2)
				Debounce = false
			end
		end
	end
end

The LocalScript is inside StarterPlayerScripts. Thanks!

1 answer

From what i got, you want fog to be changed when gamer is outside, your problem is that you don't know who is the LocalPlayer so when anyone leaves leaves the region, you will see the changes, you need to make sure that only YOU are able to change that so you need to get LocalPlayer and his Character and only allow him to be able to be detected in the region.

-->
local Gamers = game:GetService('Players')

local Gamer = Gamers.LocalPlayer
local LocalCharacter = Gamer.Character
-->


local RegionPart = workspace.RegionPart
local Pos1 = RegionPart.Position - (RegionPart.Size / 2)
local Pos2 = RegionPart.Position + (RegionPart.Size / 2)

local Region = Region3.new(Pos1, Pos2)
RegionPart:Destroy()

local Debounce = false
local Lighting = game:GetService("Lighting")

while true do
	wait()
	local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
	for _, Part in pairs(PartsInRegion) do
		if Part.Parent:FindFirstChild("Humanoid") ~= nil then
			local Character = Part.Parent
			-->
			if (Character ~= LocalCharacter) then
				continue
			end
			-->
			if not (Debounce) then
				...

i marked all edits with -->

I used continue "keyword" which skips current loop part so let's say for example

local n = 0

for i = 1, 1000 do
	if (i > 500) then
		continue
	end

	n = i
end

print(n) -- 500

It won't skip the whole loop but just current loop section. Read this you must if you don't understand it.

6 comments

1

A further note on efficiency, would be to apply ipairs here.

0

There seems to be an issue with the continue part, It doesn't seem to be able to get past that specific part, I've tried to swap it out with a print statement, and only then it was able to continue, also, testing with 2 clients, the fog still resets for both clients.

0

Oh wait I am stupid, on line 5 the character is most likely nil as its not yet loaded, what I would do is change line 26 to: if (Character ~= Gamer.Character) then... that will get current character. and yeah the ipairs is important in this case not only because of performance, but also if some part in loop becomes nil, it won't error. you should definitely use it

0

Okay, I'll try that, so far it seems to be going good, now I gotta hope it works with more than one client

0

Just as I expected, it starts to bug out when one or more players are into that region or leave

0

It should not have to do something with this script as it should not have problems, it could be script that [IsOutside] value or any other. What also stupid but possible could be region limit, you have it on 1000, that could explain problem with other clients but the first option is most likely i think. Otherwise i don't know.

This discussion is preserved as it was. New answers, comments, and votes are closed.