Region3 script error

Hello the output for my region3 script says, “Workspace.RegionPart.RegionScript:8: Expected identifier when parsing expression, got Unicode character U+2028 (did you mean ’ '?)” It also highlights the first letter of a few tasks. What I want the region to do is when you stand in it for 1 minute you gain 1 cash. I wrote code for that but I’m not confident in that so if you notice something wrong with it please correct it, (it’s near the bottom.)

local RegionPart = game.Workspace.RegionPart
local pos1 = RegionPart.Position - (RegionPart.Size /2)
local pos2 = RegionPart.Position + (RegionPart.Size/2)
local region = Region3.new(pos1, pos2)
while true do
	task.wait()
local partsInRegion = workspace:FindPartsInRegion3(region, RegionPart, 1000)

local found = {}
	

for i, part in pairs(partsInRegion) do
		local player = players:GetPlayerFromCharacter(part.Parent)
		

if player and not table.find(found, player) then
			table.insert(found, player)
			while true do
				game.Players.PlayerAdded:Connect(function(player)
					local Money = player:FindFirstChild("Money")
					local Player = game.Players.LocalPlayer
					wait(1)
					Player.Money.Value = Player.Money.Value + 1
				end)
			end
			

print("player found in the region!: " .. player.Name)
				
		end
	end
end

This should fix it. It just a stupid roblox glitch.

2 Likes
local Players = game:GetService("Players")
local RegionPart = workspace:WaitForChild("RegionPart")
local pos1 = RegionPart.Position - (RegionPart.Size/2)
local pos2 = RegionPart.Position + (RegionPart.Size/2)
local region = Region3.new(pos1, pos2)

while task.wait() do
	local partsInRegion = workspace:FindPartsInRegion3(region, RegionPart, 1000)
	local found = {}
	for i, part in ipairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") then
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				if not table.find(found, player) then
					table.insert(found, player)
					local money = player:FindFirstChild("Money")
					if money then
						task.spawn(function()
							while task.wait(1) do
								money.Value += 1
							end
						end)
					print("Player found in the region!: " .. player.Name)
				end
			end
		end
	end
end

A couple of issues with the original script, you don’t need the PlayerAdded event inside the while task.wait() do loop as you already have the player object from “GetPlayerFromCharacter”, also after the first player is inserted into the array named “found” the callback is connected to the PlayerAdded event and every player thereafter will have their money stat increased by 1 every second. Secondly “game.Players.LocalPlayer” is referenced even though it isn’t defined in server scripts. I’ve also made a few other minor optimisations.

2 Likes

It doesn’t work but I still have a couple questions. What are tasks and their importance? and what does += mean?

+= is a syntax way to basically add things without doing something like this:

Player.leaderstats.Value = Player.leaderstats.Value + 1

Instead, use +=, like this:

Player.leaderstats.Value += 1

These would do the same thing, just less repetition.

1 Like

It was working on my end but a lot of it seems unnecessary.

local Players = game:GetService("Players")
local RegionPart = workspace:WaitForChild("RegionPart")
local pos1 = RegionPart.Position - (RegionPart.Size/2)
local pos2 = RegionPart.Position + (RegionPart.Size/2)
local region = Region3.new(pos1, pos2)

while task.wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3(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")
				if money then
					money.Value += 1
				end
			end
		end
	end
end

Sorry for replying so late! It doesn’t work but I realized a big mistake. I already have a part to be the region and in the script I made a new one. I made changes but still nothing. I looked and understood your script and I don’t understand why it won’t work. The only thing in the output says unable to cast instance to region3.

I pasted it into google and I’m trying to fix it but my internet isn’t working so you don’t have to do that. I’ll try to fix it when my internet is better