Wanted Value keeps being created 2 times every time a player is considered Wanted

You can write your topic however you want, but you need to answer these questions:
Hello Developers, I’ve been trying to create a wanted system and I’m mostly having trouble giving the Player a wanted value to check if the player is wanted.

PROBLEM

image


I’ve tried to look for solutions, but not a single one is related to my problem.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

MODULE SCRIPT

 module.wanted = function()
	local mostKillsPlayer = calculateKills()

	for _, player in pairs(game.Players:GetPlayers()) do
		local wantedTag = player:FindFirstChild("Wanted")
		local bounty = player:FindFirstChild("Bounty")

		if wantedTag and bounty then
			if player ~= mostKillsPlayer or (player == mostKillsPlayer and wantedTag.Parent ~= player and bounty.Parent ~= player) then
				wantedTag:Destroy()
				bounty:Destroy()
			end
		elseif player == mostKillsPlayer then
			task.wait(0.5)
			if not wantedTag or player:FindFirstChild("Wanted") then
				local content = game.Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)

				local newWantedTag = Instance.new("BoolValue")
				newWantedTag.Name = "Wanted"
				newWantedTag.Parent = player

				if player.Character then
					player.Character.Humanoid.Died:Connect(function() --reset the wanted player on death
						if player:FindFirstChild("Wanted") then
							player.ServerKills.Value = 0
						end
					end)
				end
				--[[
				workspace.Wanted.WantedBillboard.Side1.SurfaceGui.Username.Text =  player.Name
				workspace.Wanted.WantedBillboard.Side2.SurfaceGui.Username.Text =  player.Name
				workspace.Wanted.WantedBillboard.Side1.SurfaceGui.ImageLabel.Image = content
				workspace.Wanted.WantedBillboard.Side2.SurfaceGui.ImageLabel.Image = content
				
				]]
				
			end
		end
	end
	
end

SERVER SCRIPT

local debounce = {}

game.Players.PlayerAdded:Connect(function(player)
	debounce[player] = false
	
	player.CharacterAdded:Connect(function()
		serverFunctions.wanted()
		updateBounty(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	serverFunctions.wanted()
	updateBounty(player)
	
	local wanted = player:FindFirstChild("Wanted")
	local bounty = player:FindFirstChild("Bounty")
	
	if wanted then
		wanted:Destroy()
	end
	
	if bounty then
		bounty:Destroy()
	end
end)

game:GetService("RunService").Stepped:Connect(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		if not debounce[player] then
			debounce[player] = true
			serverFunctions.wanted()
			updateBounty(player)
			task.wait(1)
			debounce[player] = false
		end
	end
	
end)

Have you hooked up some debug print statements to see how your code is progressing? I’m immediately suspicious of the FindFirstChild statements - is your code flow such that a replication delay on player join could be causing them to return nil? A quick way to check would be replacing them with WaitForChild and seeing if you get the same problem.

try adding an if check to see if there is an already existing wanted value.
also, i recommend putting all of the player’s values inside of a separate folder to clear up some space.

Well now it returns as an Infinite yield possible on the instance, and there are no delays on the player join either.

Yes, there’s a check on the players wanted status. I would disagree putting them in a folder because it’s easy enough to check the status

I think your operators may not be doing what you want them to do. This line:

if not wantedTag or player:FindFirstChild("Wanted") then

Means “Do this code if wantedTag doesn’t exist, or if you find an object in the Player called Wanted.” I don’t think that’s what you want. This line means “If you can’t find wantedTag or an object called Wanted”, which I think is what you want. It’s probably what’s duplicating your objects.

if not (wantedTag or player:FindFirstChild("Wanted")) then

I’m also a little confused about what you’re doing. To me, your module looks like it does the following:

  1. Do some math to see who has the most kills.
  2. For each player, check for a wanted tag and a bounty.
  3. If a player with a wanted tag and a bounty doesn’t have the most kills, delete the wanted tag and the bounty.
  4. If a player with the most kills does not have a wanted tag and a bounty, wait half a second, check again (?), and if they still don’t, give them new ones.
  5. Connect an event so that if that player dies, their kills are reset to 0.

The Wanted tag is a boolean value - you can set it to true or false. If I were doing this, I’d give every player a Wanted value and a Bounty in a simple playerAdded function, then if a player doesn’t have the most kills, set Wanted to false and Bounty to zero. Then when you want to check to see if a player is wanted, you can directly reference it like this:

if player.Wanted.Value then
    (wanted things)

and it won’t break if you run it on a player who isn’t wanted.

1 Like

Okay, that will work much better anyways. Thank you for taking your time to help me out with this.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.