For loop returning an incorrect value?

hi people so i got an issue again

				local lastSpeed = plr.Stats.Movespeed.Value  --16
				local lastPriority = 0
				print(AddedSpeeds)
				for index,value in pairs(AddedSpeeds) do 
					if value.Priority > lastPriority then
						lastSpeed = value.Speed
						lastPriority = value.Priority
					end 
					if value.Speed <=lastSpeed and value.Priority <lastPriority  then
						lastSpeed = value.Speed
					end
				end
				print(lastSpeed)

this is supposed to returning either the lowest speed or highest priority right
for reference here’s what addedspeeds is equal to:
image

but for some reason the last speed is set to 16 instead of 36.5?

image

i’ve ran this code to simulate the issue on a blank baseplate:


local dictionary = {
	["Base"] = {
		Speed = 0;
		Priority = 0;
	},
	["Test"] = {
		Speed = 30;
		Priority = 2;
	}
}

local lastPriority = 0
local lastSpeed = 16

print(dictionary)
for index,value in pairs(dictionary) do 
	if value.Priority > lastPriority then
		lastSpeed = value.Speed
		lastPriority = value.Priority
	end 
	if value.Speed <=lastSpeed and value.Priority <lastPriority  then
		lastSpeed = value.Speed
	end
end
print(lastSpeed)

however that code returns 30 which is the correct one (higher priority)
image

i must be missing something in the first code to cause this issue any help is appreciated!

edit - sent a wrong code on the second thingy corrected

1 Like

What’s your table for the first code?

1 Like

its shown in the first screenshot of the output, its solved now anyway by another person how exactly do i mark the post as solved now?

1 Like

I’m pretty sure remove this part:

if value.Speed <=lastSpeed and value.Priority <lastPriority  then
						lastSpeed = value.Speed
					end
1 Like

Yes, if you dont mind me asking what was the fix?

1 Like
				local slowest;
				local slowestData;
				local highestPriority = 0
				local datum = {}

				for _, data in pairs(AddedSpeeds) do
					local currentPriority = data.Priority

					if currentPriority == highestPriority then
						table.insert(datum, data)
					elseif currentPriority > highestPriority then
						highestPriority = currentPriority
						datum = {
							data
						}
					end
				end

				for _, data in pairs(datum) do
					local currentSpeed = data.Speed

					if not slowest then
						slowest = data
					elseif currentSpeed < slowest.Speed then
						slowest = data
					end
				end
				hum.WalkSpeed = slowest.Speed

by someone from discord yeah

2 Likes