Region3 Players Issue

Hello guys, recently I’ve been working on a button system where i use region3’s to Define if the player is on the button. The point is to give/change the stats of the players inside the region3.

  1. What do you want to achieve? Change the stats of all players in a same region!

  2. What is the issue? If 2 players are standing in a region (button) it would only change the stats to one of the ‘x’ players

  3. What solutions have you tried so far? I’ve tried alot of things and i know the issue is that i never told the script to change everyone’s stat in a region. I don’t know how to do that

while true do
	wait()
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			local char = part.Parent
			local player = game.Players:GetPlayerFromCharacter(char)
			while true do
				if (RegionPart.Position - char:WaitForChild("HumanoidRootPart").Position).Magnitude >= 4 then
					break -- This is just for people that leave the button's area it will stop the loop
				end
				wait(0.1)
				if player.Leaderstats.Coins.Value >= amount then -- Stat changes
					player.Leaderstats.Coins.Value -= amount
					player.Leaderstats.Rebirths.Value += rebirths
					player.Leaderstats.Tickets.Value += tickets
				end
			end
		end
	end
end

The script above is shortened to the only main part

Summary: The script above only changes the stats on 1 of ‘x’ players standing on the button and i don’t know how to fix it.

Thanks for any help in advance!

1 Like

Get rid of the inner secondary loop, change your main wait to 0.1 if that’s the gap you want each time between changing the values, and double check the player exists before you start trying to access their Leaderstats, just in case you’ve caught an NPC or something else causes it not to find the player.

while true do
	wait(0.1)
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			local char = part.Parent
			local player = game.Players:GetPlayerFromCharacter(char)
			if player and player:FindFirstChild('Leaderstats') then
				if player.Leaderstats.Coins.Value >= amount then -- Stat changes
					player.Leaderstats.Coins.Value -= amount
					player.Leaderstats.Rebirths.Value += rebirths
					player.Leaderstats.Tickets.Value += tickets
				end
			end
		end
	end
end

With the code you presented, due to the inner loop, it will never process the next player until the first one leaves the button. This solution gets the players each time so you don’t need the magnitude check (which was comparing a sphere, so would return different to your cuboid region check) and avoids the need for a loop in a loop.

3 Likes

Holy, thanks man, i was so stupid, Tysm <3