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

[SOLVED] Addition not doing anything for unknown reason?

Whenever I try to do simple addition it just keeps the number the same as if I never added anything. I can verify that it is running the line of the script where I do the addition because I have a print immediately before it which prints when I run the script.

This is where the addition takes place:

print("recently rolled so increasing max distance") --Prints correctly
MaxDistance += 25 --Doesn't do anything but also has no errors

The entire loop this is happening in is:

while LoopActive == true do
	wait(0.33)
	local RootPart = Character:FindFirstChild("HumanoidRootPart")
	if RootPart ~= nil then
		if StaminaTable[Character.Name] ~= nil and StaminaTable[Character.Name].Stamina < 12 then
			StaminaTable[Character.Name].Stamina += 1
			PhysicalActionEvent:FireClient(Player, StaminaTable[Character.Name].Stamina)
		end
		if StaminaTable[Character.Name] ~= nil then
			StaminaTable[Character.Name].CD = false
		end
		if DelayedPreviousPositions[Character.Name] ~= nil then
			local PreviousPos = DelayedPreviousPositions[Character.Name]
			PreviousPos = Vector3.new(PreviousPos.X, 0, PreviousPos.Z)
			local MaxDistance = (Character.Humanoid.WalkSpeed * 0.33) + 4
			local SeatPart = Character.Humanoid.SeatPart
			if SeatPart ~= nil then
				if SeatPart.ClassName == "VehicleSeat" then
					MaxDistance = (SeatPart.MaxSpeed * 0.33) + 12
				elseif SeatPart:IsDescendantOf(game.Workspace.Vehicles) ~= true then
					MaxDistance = (SeatPart.Parent.VehicleSeat.MaxSpeed * 0.33) + 4
				else
					MaxDistance = 99999
				end
			elseif Player:FindFirstChild("RecentlyRolled") ~= nil and Player.RecentlyRolled.Value == true then
				print("recently rolled so increasing max distance")
				MaxDistance += 25 --This is the addition which isn't working
			end
			local Distance = (PreviousPos - Vector3.new(RootPart.Position.X, 0, RootPart.Position.Z)).Magnitude
			if Distance > MaxDistance then
				print(Distance, " was greater than ", MaxDistance)
				RootPart.Position = DelayedPreviousPositions[Character.Name]
			end
		end
		DelayedPreviousPositions[Character.Name] = RootPart.Position
	end
end

I have tried replacing

MaxDistance += 25

with

MaxDistance  = MaxDistance  + 25

but that didn't fix anything.

The script runs completely without errors and all the prints print correctly, yet it seems as if it is incapable of addition in that one spot for some unknown reason.

UPDATE: This only happens sometimes, the majority of the time it is able to use the value with the addition, but occasionally it uses the old value (yes it is adding to the value I verified with prints)

8 comments

0

It's probably Luau just being special for no apparent reason. Use value = value + n.

0

tried that it didn't change anything

0

If you're not printing MaxDistance then you don't actually know if it changed. The code you're showing us has no instance of you doing this. Because the messages print you're only assuming the addition is being done. Nothing can be proved without actually printing the value.

0

I did print the value

0

look at line 31

0

One print isn't enough.

0

what do you mean? There would be no point in printing the same thing multiple times lol

0

Nevremind, I understand what you meant now and I put another print so I can get the value before additon, after the addition, and the value used after all the addition is done. I discovered that roughly 15% of the time the value used after the addition is the value pre-addition which makes no sense as it no longer exists. Even when this happens it still prints the post-addition value correctly.

1 answer

Not sure if MaxDistance was a NumberValue or created locally in the script. If it is a NumberValue or IntValue then try adding .Value

MaxDistance.Value = MaxDistance.Value + 25

Otherwise, I'm not sure what is wrong.

4 comments

0

it is just a variable in the script which is always a number

0

Oh, I see. I have no idea why it's not working. Maybe that 99999 value messes it up. I tried adding 99999 to randoms numbers before, and it sometimes prints as 0 when I use the print statement. Maybe consider using 9999 instead?

0

that part of the script has never been run (i never entered a passenger seat while testing this so far), but thanks for the heads-up

0

Alright

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