I have a problem with regions

So i am trying to make a king of the hill type of region but i want everyone in that region to earn a crown.The problem is that only 1 person gets crown and not the others.
here is code:

local kingPart = workspace.KingOfTheHillPart
local region = Region3.new(kingPart.Position - (kingPart.Size/2),kingPart.Position + (kingPart.Size/2))
local foundPlr = false
local plr = nil
local db = false

while wait(2) do
	local PartsinRegion = workspace:FindPartsInRegion3(region,nil)
	for i,v in pairs(PartsinRegion) do
		plr = game.Players:GetPlayerFromCharacter(v.Parent)
		if plr then
			foundPlr = true
			break
		elseif not plr then
			foundPlr = false
		end
	end

	if foundPlr == true then
		print(plr.Name)
		plr.Crowns.Value += 1
		
	end
end
local kingPart = workspace.KingOfTheHillPart
local region = Region3.new(kingPart.Position - (kingPart.Size/2),kingPart.Position + (kingPart.Size/2))
local foundPlr = false
local plr = nil
local db = false

while wait(2) do
	local PartsinRegion = workspace:FindPartsInRegion3(region,nil)
	for i,v in pairs(PartsinRegion) do
		plr = game.Players:GetPlayerFromCharacter(v.Parent)
		if plr then
			plr.Crowns.Value += 1
		end
	end
end

This should fix your problem. You were looping through all the players that were found and when you found one, you exited the loop and then awarded that one player a crown. Now the script will not stop once it find a player. It will award a crown to each player found.

1 Like

i am gonna test it and see if it works give me a sec

it gives 16 crowns every 2 seconds

Don’t set foundPlr to false inside the for loop, it will be false unless if you find a player in the loop.
Also, to give the crown to multiple people make a dictionary where the players’ instance = true so you know who was in the region.

local dictionary = {}

–
dictionary = {}
for i,v in pairs(PartsinRegion) do
plr = game.Players:GetPlayerFromCharacter(v.Parent)
if plr then
foundPlr = true
dictionary[plr] = true
end
end
for plr,val in pairs(dictionary) do
–award crowns
end

1 Like

Your solution will award a crown per body part the player has in the region. Not 1 per player.

1 Like

Ahh that is expected. As players have multiple parts. You should check if you already awarded a player a crown. I only fixed the problem you were having, not redesigned your code.

1 Like

You are getting 16 crowns because it gives one for each body part. To stop this you can check if the part’s name is “Head”. Because every character only has 1 head. And this works for R6 and R15.

Change of the script @hamsterloverboy3 send

local kingPart = workspace.KingOfTheHillPart
local region = Region3.new(kingPart.Position - (kingPart.Size/2),kingPart.Position + (kingPart.Size/2))
local foundPlr = false
local plr = nil
local db = false

while wait(2) do
	local PartsinRegion = workspace:FindPartsInRegion3(region,nil)
	for i,v in pairs(PartsinRegion) do
        if v.Name == "Head" then
			plr = game.Players:GetPlayerFromCharacter(v.Parent)
			if plr then
				plr.Crowns.Value += 1
			end
		end
	end
end
2 Likes

so your saying i should make a dictionary with each player’s name inside the region?

Yeah just do dictionary[plr] = true and then loop through the dictionary (where the plr instance is the index) to award a crown per person.^
Don’t forget to set the dictionary to {} in the beginning of the while loop

1 Like

ok i am gonna test this with a friend and see if it works

Checking the head seems to be a solid solution. I think OP is trying to create a game where people get crowns every two seconds if they are standing on the hill.

1 Like

Mark it as the solution if it worked. This will help other people also find the fix to the problem!

1 Like

alright i am testing rn if it works i will mark it

Better / More organized solution

local MainPart = game.Workspace.KingOfTheHillPart
local MainRegion = Region3.new(MainPart.Position - (MainPart.Size/2), MainPart.Position + (MainPart.Size/2))

while wait(2) do
    local PartsinRegion = game.Workspace:FindPartsInRegion3(MainRegion, MainPart, 1000000000)
    for Index, CurrentObject in pairs(PartsinRegion) do
        if CurrentObject.Name == "Head" then
            local CurrentPlayer = game.Players:GetPlayerFromCharacter(CurrentObject.Parent)
            if CurrentPlayer then
                CurrentPlayer.Crowns.Value += 1
            end
        end
    end
end
1 Like

thanks a lot for the help @Dutch_Coder @hamsterloverboy3 and @MartimDev :+1:

1 Like