Wanted system only works for first player

Im making a town-rpg game something like generic roleplay gaem and a wanted system for the game. Basically when a player comitted a crime, i want them to become wanted for a while. It’s working well when i tested by myself but in team test, it only works for the first player who increased “WantedTime” intvalue

local folder = game.ReplicatedStorage.MoreStuffs
local functionrunning = false
local humanoiddied = false

local function wantedazaltma(wantedtime)

	print("function 1")

	if humanoiddied == true then
		print("return 1")
		humanoiddied = false
		functionrunning = false

		return
	end

	if wantedtime then
		if humanoiddied == true then
			print("return 2")
			humanoiddied = false
			functionrunning = false

			return
		end
	end

	while wantedtime.Value > 0 do

		if humanoiddied == true then
			humanoiddied = false
			functionrunning = false
			print("return 3")
			return
		end

		if wantedtime then
			wait(1)
			print("while")
			wantedtime.Value -= 1

		end

	end


	functionrunning = false

end


game:GetService("Players").PlayerAdded:Connect(function(plr)

	plr.CharacterAdded:Connect(function(char)
		humanoiddied = false
		print("char added")
		local wantedbillboard = folder.WantedBillboard:Clone()
		wantedbillboard.Parent = char:WaitForChild("Head")

		local wantedbool = Instance.new("BoolValue")
		wantedbool.Value = false
		wantedbool.Name = "WantedBool"
		wantedbool.Parent = char

		local wantedtime = Instance.new("IntValue")
		wantedtime.Value = 0
		wantedtime.Name = "WantedTime"
		wantedtime.Parent = char

		local connectionazaltma

		local function wantedtimechanged()

			if wantedtime.Value < 0  then

				wantedtime.Value = 0
				wantedbool.Value = false

			elseif wantedtime.Value == 0 then

				wantedbool.Value = false

			elseif wantedtime.Value > 0 then

				wantedbool.Value = true

				if functionrunning == false then
					functionrunning = true
					spawn(function()
						print("spawn")
						wantedazaltma(wantedtime)
					end)
				end

			end

			if wantedtime.Value > 0 and wantedtime.Value <= 240 then
				wantedbillboard.TextLabel.Text = "★"
			elseif wantedtime.Value > 240 and wantedtime.Value <= 480 then
				wantedbillboard.TextLabel.Text = "★★"
			elseif wantedtime.Value > 480 and wantedtime.Value <= 720 then
				wantedbillboard.TextLabel.Text = "★★★"
			elseif wantedtime.Value > 720 and wantedtime.Value <= 960 then
				wantedbillboard.TextLabel.Text = "★★★★"
			elseif wantedtime.Value > 960 then
				wantedbillboard.TextLabel.Text = "★★★★★"

			end
		end

		local wantedtimeconnect = wantedtime.Changed:Connect(wantedtimechanged)

		local function wantedboolchanged()

			if wantedbool.Value == true then
				wantedbillboard.Enabled = true
			else
				wantedbillboard.Enabled = false
			end

		end

		local wantedboolconnect = wantedbool.Changed:Connect(wantedboolchanged)
		local hum = char:FindFirstChild("Humanoid")

		hum.Died:Connect(function()
			humanoiddied = true
			wantedtimeconnect:Disconnect()

		end)


	end)
end)
```.
1 Like

humanoiddied and functionrunning aren’t variables local to the player. Redefine them when you connect to PlayerAdded, and pass them through as arguments in wantedazaltma().

2 Likes

i made the value 10 and waited for it to be 0, made it 10 again in test but it didnt reduce this time. I guess functionrunning is not set to false

1 Like

okay i removed the wantedazaltma() function and put the script in spawn it seems to be working right now

1 Like

That will not completely solve your issue. You can accept the variables as arguments for your wantedazaltma() function and instead of making global variables, create the variables inside the PlayerAdded event which’ll convert it to a local variable.

2 Likes

like i said when i tried this

then i removed the wantedazaltma() function and put the scripts in spawn. it works well now

You might want to try loop through all the player’s. (If you are already doing this or have done this,sorry.)


local players = game:GetService("Players")

for index,player in pairs(players:GetPlayers()) do
-- Do something.
end

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