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

How to increase a leaderstats value by 1 for only certain players?

Hi again! I know I've posted a lot of questions recently, but am fairly new to roblox scripting. What I've got is when the timer runs out, I check all the parts in a Region3. I then check which of those are players. I after that I get the leaderstats of those players. But when running wins.Value = wins.Value + 1 it adds 14 to my Wins. Any idea on how to fix this? I cant use break because I still need to teleport them to the lobby. I don't actually even know if break will work.

The part of the script as requested:

		local partsInHitbox = workspace:FindPartsInRegion3WithIgnoreList(hitboxRegion, 				playingField:GetChildren())
		for i, v in pairs(partsInHitbox) do
			local human = v.Parent:FindFirstChild("Humanoid")
			if human then
				local char = human.Parent
				local plr = game.Players:GetPlayerFromCharacter(char)
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats then
					local wins = leaderstats:FindFirstChild("Wins")
					if wins then
						wins.Value = wins.Value + 1
					end
				end
			end
		end

Any help appreciated!

13 comments

0

Would you mind posting the part of the script? It would be easier to locate the problem that way.

0

Ok!

0

Done!

0

To be honest, I don't know why it's doing that, try adding a delay to it.

0

Before or After?

0

Add a delay after you add a win to the player

0

I just did the normal wait() and all it did was the same but slightly slower

0

hmm. Weird.

0

I really don't know, sorry.

0

Its ok

0

Someone will

0

Did you try to print?

0

print what?

1 answer

✓ Accepted answer

it's because roblox characters do not have just one part, they have multiple so it does this function for every single part in that region. Easy fix for you is to check if part's name is HumanoidRootPart as it is the most important part of character and you should always use it as a hitbox. So instead of checking for humanoid (Because 15 parts in character have humanoid as v.Parent.Humanoid), check if part's name is HumanoidRootPart:

local partsInHitbox = workspace:FindPartsInRegion3WithIgnoreList(hitboxRegion,              playingField:GetChildren())
for i, v in pairs(partsInHitbox) do
    if v.Name == "HumanoidRootPart" then -- since player's only have one, it will no longer duplicate
        local char = v.Parent
        local plr = game.Players:GetPlayerFromCharacter(char)
        local leaderstats = plr:FindFirstChild("leaderstats")
        if leaderstats then
            local wins = leaderstats:FindFirstChild("Wins")
            if wins then
                wins.Value = wins.Value + 1
            end
        end
    end
end

1 comment

0

Sorry, did it wrong, works perfectly!

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