Aparent Schrödinger's Script situation?

While programming a locomotive’s Distributed Power Utility or DPU System I am noticing that the Variable (Mimic) that sets up the locomotive’s ability to mimic the lead locomotive somehow is reading both True and False at the same time. I have checked over the script and it’s not changing it somewhere down the line, it’s genuinely just reading both true and false somehow.

Some of the things I have tested and results:

  • Added a “.Changed()” function to see if its getting changed randomly. Rsesult: It’s only being changed at the DPU Setup function.
  • Put printouts in the DPU Setup, Mime Function, and the While True Do Loop. Result: DPU Setup reads true, Mime Function Reads false, While True Do reads true
  • Checked for duplicate variables. Result: No duplicates found.

Script:

-- Motive Works Drive System Controls V4
--- Created: 1/13/2024
---- Original Programer: Sterling_ODeaghaidh(10283957)
---------------Configuration Variables----------------

-- Determines the max output of the vectors.
Max                   = 100
Min                   = -100
--                    --
Platform              = script.Parent
UI                    = Platform.Parent.Parent.Parent:WaitForChild("Errors")
Locomotive            = Platform.Parent.Parent.Parent
LocomotiveBody        = Locomotive.ConfigurationItems.bOdy.Value
Acceleration          = 500
MaxForce              = 500000
MaxTorque             = 17976931348623157081452742373170435679807056752584499659891747680315
Accelerate            = Max * 0.001
Decelerate            = Min * -0.001
BrakingForce          = 100000

----------------------Global Variables------------------

loop                  = true
Mode                  = false
Mime                  = false
Speed                 = "Null"
Accel                 = 0
LocomotiveFound       = false
Face                  = "Forward"
Wheels                = {}
Priority              = LocomotiveBody
ActiveTweens          = {}

-------------------------------Begin Main Code--------

--setup
Platform.Parent.Dash.Max.Value = Max
Platform.Parent.Dash.Min.Value = Min
Platform["Regen and Airbrakes"].Max.Value = Max
Platform["Regen and Airbrakes"].Min.Value = Min

-- DPU Setup
Platform.DMU.OnServerEvent:Connect(function(P,C)

	if loop == true then
		loop = false
		for index, Locomotive1 in pairs(game.Workspace:GetChildren()) do
			if Locomotive1 == C then
				Mime = true
				Priority = Locomotive1.ConfigurationItems.bOdy.Value
				Locomotive.Values.PairedEngine.Value = Locomotive1
				LocomotiveFound = true
				Locomotive.Values.Connected.Value = true
				----
				break
			end
		end
		----
		if LocomotiveFound == false then
			local errorLabel = Instance.new("StringValue")
			errorLabel.Name = "Error"
			errorLabel.Value = "DPU Error: No Locomotive Found"
			errorLabel.Parent = UI
		end
	else
		loop = true
		Mime = false
		Priority = LocomotiveBody
		Locomotive.Values.PairedEngine.Value = Locomotive
		Locomotive.Values.Connected.Value = false
	end

end)

-- Tow Mode Function
Platform.Dummy.OnServerEvent:Connect(function()

	LocomotiveBody.SoundEmitter.Click:Play()
	if Mode == false then 
		Mode = true
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				child.ActuatorType = Enum.ActuatorType.None
				Locomotive.Values.Tow.Value = true
			end
		end
	elseif Mode == true then
		Mode = false
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				child.ActuatorType = Enum.ActuatorType.Motor
				Locomotive.Values.Tow.Value = false
			end
		end
	end

end)

-- Accelerate Function
local function up()
	if loop == true then
		loop = false
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				if child.AngularVelocity < Max then
					child.MotorMaxAcceleration = Acceleration
					child.MotorMaxTorque = MaxTorque
					child.AngularVelocity += Vector3.new(100,0,0)
				end
			end
		end
		wait(0)
		loop = true
	end 
end

-- Decelerate Function
local function Down()
	if loop == true then
		loop = false
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				if child.AngularVelocity > Min then
					child.MotorMaxAcceleration = Acceleration
					child.MotorMaxTorque = MaxTorque
					child.AngularVelocity -= 0.05
				end
			end
		end
		wait(0)
		loop = true
	end 
end

-- Emergency Brake Function
local function stop()
	if Mode == false then
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				LocomotiveBody.SoundEmitter.Burst:Play()
				Locomotive.Values.Air.Value = 0
				child.AngularVelocity = 0
				child.MotorMaxTorque = 0
				child.MotorMaxAcceleration = 0
			end
		end
	end
end
----
local function stop1()
	if Mode == false then
		for index, child in pairs(Platform:GetChildren()) do
			if child:IsA("HingeConstraint") then
				child.AngularVelocity = 0
				child.MotorMaxTorque = 0
				child.MotorMaxAcceleration = 0
			end
		end
	end
end

-- Chanes Direction Value
Platform.Parent.Direction.OnServerEvent:Connect(function()

	if Platform.Parent.Front.Value == true then
		Platform.Parent.Front.Value = false
	else
		Platform.Parent.Front.Value = true
	end

end)

-- Connects to Accelerate Function
Platform.Accelerate.OnServerEvent:Connect(function(P,N)

	Accel = N
	if Accel == 0 then
		LocomotiveBody.SoundEmitter.SlowRelease:Stop()
		Platform["Regen and Airbrakes"].Enabled = false
	end

end)

-- Displays the power setting.
Platform.H1.Changed:Connect(function()

	if Locomotive.Values.Front.Value == false then
		Locomotive.Values.Power.Value = math.floor((Platform.H1.AngularVelocity / tonumber(Min) * 100) * 10 + 0.5) / 10
	else
		Locomotive.Values.Power.Value = math.floor((Platform.H1.AngularVelocity / tonumber(Min) * -100) * 10 + 0.5) / 10
	end

end)
----
Platform.Brake.OnServerEvent:Connect(stop)
----
Platform.SoftBrake.OnServerEvent:Connect(stop1)


-- Mime Function
Priority.locoSeat.Platform.H1.Changed:Connect(function(Object)
	print(Mime)
	print(1)
	if Mime == true then
		print(2)
		if Object == "AngularVelocity" then
			print(3)
			for index, velocity in pairs(Platform:GetChildren()) do
				if velocity:IsA("HingeConstraint") then
					local OriginalVelocity = velocity.AngularVelocity
					if Locomotive.Values.Front.Value then
						velocity.AngularVelocity = Priority.locoSeat.Platform.H1.AngularVelocity
						velocity.MotorMaxTorque = Priority.locoSeat.Platform.H1.MotorMaxTorque
						velocity.MotorMaxAcceleration = Priority.locoSeat.Platform.H1.MotorMaxAcceleration
					else
						velocity.AngularVelocity = -Priority.locoSeat.Platform.H1.AngularVelocity
						velocity.MotorMaxTorque = Priority.locoSeat.Platform.H1.MotorMaxTorque
						velocity.MotorMaxAcceleration = Priority.locoSeat.Platform.H1.MotorMaxAcceleration
					end
				end
			end
		end
		print(4)
		----
		if Locomotive.Values.Engine.Value == true then
			LocomotiveBody.Exhaust.Cloud.Enabled = true
			wait(3)
			LocomotiveBody.Exhaust.Cloud.Enabled = false
		end
	end
	
end)

-- Repeating Loop
while true do
	
	wait()
	----
	if Locomotive.Values.Air.Value >= 40 then
		if Accel == 1 then
			for index, child in pairs(Platform:GetChildren()) do
				if child:IsA("HingeConstraint") then
					if child.AngularVelocity < Max then
						if Locomotive.Values.Front.Value == true then
							local Downforce = child.AngularVelocity * 0.001
							if Platform.H1.AngularVelocity >= 0 then
								child.MotorMaxAcceleration = Acceleration
								child.MotorMaxTorque = MaxTorque
								child.AngularVelocity += 0.1
							else
								child.AngularVelocity = 0
							end
						else
							if Platform.H1.AngularVelocity <= 0  then
								local Downforce = child.AngularVelocity * 0.001
								child.MotorMaxAcceleration = Acceleration
								child.MotorMaxTorque = MaxTorque
								child.AngularVelocity -= .1
							else
								child.AngularVelocity = 0
							end
						end
					end
				end
			end
		elseif Accel == -1 then
			Platform["Regen and Airbrakes"].Enabled = true
			----
			for index, child in pairs(Platform:GetChildren()) do
				if child:IsA("HingeConstraint") then
					if child.AngularVelocity > Min then
						if Locomotive.Values.Front.Value == true then
							if Platform.H1.AngularVelocity > 0 then
								child.MotorMaxAcceleration = Acceleration
								child.MotorMaxTorque = MaxTorque
								child.AngularVelocity -= 0.1
							else
								child.AngularVelocity = 0
							end
						else
							if Platform.H1.AngularVelocity < 0 then
								child.MotorMaxAcceleration = Acceleration
								child.MotorMaxTorque = MaxTorque
								child.AngularVelocity += .1
							else
								child.AngularVelocity = 0
							end
						end
					end
				end
			end
		end
	end
	----
	if Locomotive.Values.Isolator.Value ~= 1 then
		if Locomotive.Values.Electrics.Value == true then
			Locomotive.Values.Bat.Value -= .01
		end
	end
	----
	if Platform.H1.AngularVelocity > 0 then
		for i, c in pairs(Locomotive:GetDescendants()) do 
			if c.Name == "Buzz" then
				if c.Playing == false then
					c:Play()
				end
				c.PlaybackSpeed = .3 + (Platform.H1.AngularVelocity / Max)
				c.Volume = 1
			end
		end
		LocomotiveBody.Fan.Idle.PlaybackSpeed = .5 + (Platform.H1.AngularVelocity / Max)
	elseif Platform.H1.AngularVelocity < 0 then
		for i, c in pairs(Locomotive:GetDescendants()) do 
			if c.Name == "Buzz" then
				if c.Playing == false then
					c:Play()
				end
				c.PlaybackSpeed = .3 + (Platform.H1.AngularVelocity / Min)
				c.Volume = 1
			end
		end
		LocomotiveBody.Fan.Idle.PlaybackSpeed = .5 + (Platform.H1.AngularVelocity / Min)			
	elseif Platform.H1.AngularVelocity ~= 100 then
		for i, c in pairs(Locomotive:GetDescendants()) do 
			if c.Name == "Buzz" then
				if c.Playing == true then
					c:Stop()
				end
				c.Volume = 0
			end
		end
	end
	----
	Locomotive.Values.Air.Value -= .01
	----
	if Locomotive.Values.Isolator.Value ~= 1 then
		if Locomotive.Values.Bat.Value <= 80 and Locomotive.Values.Engine.Value == true then
			Locomotive.Values.Bat.Value += 0.1
			if Locomotive.Values.Bat.Value >= 30 and LocomotiveBody.locoSeat.Platform.Control.Enabled == false then
				Platform.Control.Enabled = true
			end
		end
	end
	----
	if Locomotive.Values.Bat.Value >= 80 and Locomotive.Values.Isolator.Value == 2 and Locomotive.Values.Engine.Value == true then
		Locomotive.Values.EngineStop.Value = true
	end
	----
	if Locomotive.Values.Air.Value <= 65 and LocomotiveBody.SoundEmitter.Compressor.Playing == false then
		LocomotiveBody.SoundEmitter.CompStart:Play()
		LocomotiveBody.SoundEmitter.Compressor:Play()
	end
	----
	if Locomotive.Values.Air.Value > 90 and LocomotiveBody.SoundEmitter.Compressor.Playing == true then
		LocomotiveBody.SoundEmitter.Compressor:Stop()
		LocomotiveBody.SoundEmitter.Release:Play()
	end
	----
	if Locomotive.Values.Air.Value <= 90 and LocomotiveBody.SoundEmitter.Compressor.Playing == true then
		Locomotive.Values.Air.Value += 0.2
	end
	----
	if Locomotive.Values.Isolator.Value ~= 1 then
		if Platform.H1.AngularVelocity > 0 then
			Locomotive.Values.Bat.Value -= .01 + (Platform.H1.AngularVelocity * 0.01 / Max)

		elseif Platform.H1.AngularVelocity < 0 then
			Locomotive.Values.Bat.Value -= .01 + (Platform.H1.AngularVelocity* 0.01 / Min)		
		end
	end
	----
	if Locomotive.Values.Bat.Value < 100 and Locomotive.Values.Engine.Value == true then
		Locomotive.Values.Bat.Value += 0.05
	end
	----
	Locomotive.Values.Speed.Value = tostring(math.floor(Platform.Parent.Velocity.Magnitude))
end

I am seriously at a loss as to why this is happening.

can you check, a line below print(Mime):

if Mime == true and tostring(Mime) == true

?

Type string cannot be compared to true

I figured the issue foir some reason, the script things the Mime line in the first function is a global variable.

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