Function checks for all players, but only checks one player's value?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Hi,
While trying not to explain the game I am making, I want a possible fix for a situation I have just ran into.

Players are given rewards depending on BoolValues and StringValues. These rewards can be found in a table:

return {
	["NPC"] = {
		["Normal Hour"] = {
			[true] = {
				["Points"] = 150,
				["Kills"] = 1,
			},
			[false] = {
				["Points"] = 50,
				["Kills"] = 1,
			},
		},
		["Hard Hour"] = {
			[true] = {
				["Points"] = 450,
				["Kills"] = 1,
				["H-Kills"] = 1,
			},
			[false] = {
				["Points"] = 400,
				["Kills"] = 1,
			}
		}
	},
	["NONNPC"] = {
		["Normal Hour"] = {
			[true] = {
				["Points"] = 350,
				["Kills"] = 1,
			},
			[false] = {
				["Points"] = 150,
				["Kills"] = 1,
			},
		},
		["Hard Hour"] = {
			[true] = {
				["Points"] = 600,
				["Kills"] = 1,
				["H-Kills"] = 1,
			},
			[false] = {
				["Points"] = 450,
				["Kills"] = 1,
			}
		}
	}
}

When it comes to normal hours, they are given rewards depending on a BoolValue called “HourBeaten” when another BoolValue called “Assisted” is true. However Hard Hours are called with a different BoolValue called Survived. These values are found in a player class (so that these values differ for different people).

I have a function that looks through the Module and gives players their appropriate rewards depending on the Hour chosen, alongside the value it requires (etc. Normal Hours > Hour Beaten [true or false]). Here’s the function thats called everytime an NPC dies:

local function NPCDeath(player)
	player = players:FindFirstChild(player.Name)
	triggersFolder.isHour.Transition.Value = false
	local selectedNPC = triggersFolder.SelectedNPC.Value
	local avgPoints = assistFolder:FindFirstChild(selectedNPC).PointsAmount.Value
	local avgKills = assistFolder:FindFirstChild(selectedNPC).KillsAmount.Value
	local avgHKills = 0
	if not triggersFolder.isHour.isHHour.Value then
		if player.Assisted.Value then
			if hourName.Value ~= "" then
				avgPoints += hourDetails[selectedNPC][hourName.Value][triggersFolder.isHour.HourBeaten.Value].Points
				avgKills = hourDetails[selectedNPC][hourName.Value][triggersFolder.isHour.HourBeaten.Value].Kills
			end
			avgPoints = math.floor(avgPoints)
			avgKills = math.floor(avgKills)

			player.leaderstats.Points.Value += avgPoints
			player.leaderstats.Kills.Value += avgKills
		end
	else
		avgPoints += math.floor(hourDetails[selectedNPC][hourName.Value][player.Survived.Value].Points)
		avgKills = math.floor(hourDetails[selectedNPC][hourName.Value][player.Survived.Value].Kills)
		avgHKills = math.floor(hourDetails[selectedNPC][hourName.Value][player.Survived.Value]["H-Kills"] or 0)

		player.leaderstats.Points.Value += avgPoints
		player.leaderstats.Kills.Value += avgKills
		player.leaderstats["H-Kills"].Value += avgHKills
	end
	warn(player.Name, player.Survived.Value, avgPoints .. "P", avgKills .. "K", avgHKills .. "H")
end

The issue is that when this function is called, it gets every player possible. However, it checks one specific player and gives everybody the same reward as them. So if one doesn’t survive, the rest follow. I want it to be completely independent and check every player’s boolvalue instead of just one. Here’s a screenshot below of some debugs:


(CONTEXT: First word is the username, second is the value of Survived, third is the points received, then Kills and H-Kills.)
(EXTRA CONTEXT: The hour that occured was a Hard Hour. Vegetta survived so they received 2 H-Kills but I died (Less rewards). Because of this, everybody below me had gotten the same rewards, even if they survived.)

I have changed the function to include i, v in pairs(game:GetService("Players"):GetPlayers()) do but that did not fix the bug. I also put the player as a variable in the script (can be seen above) yet gives the same results. Is there a fix for this?

1 Like

Change that to local player =

Why are you retrieving a player Instance from the Players service…with the name of the already passed in player Instance?

1 Like

Hey there everybody! Thanks to the two that posted above I decided to dig way deeper. It turns out the isHHour value was being changed the moment this function fired so naturally not everybody was receiving the rewards.

1 Like

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