Switch isn't working properly!

Hello! So I was scripting a power laser switch for my game until I ran into a problem.
The switch was literally not working!
An example: If you’re trying to increase the power to 3, the switch goes all the way to 4
If you want to decrease the power from 3 to 2, it goes to 4 and so on.
When you want to decrease the power to 1 it just is not working!

Script
local Line = script.Parent
local ClickDetector = Line:WaitForChild("ClickDetector", 60)
local Sound = Line:WaitForChild("SwitchSound", 60)
local Values = workspace:WaitForChild("Values", 60)
local Switch = Line.Parent.Parent.Parent:WaitForChild("Switch")
local BeingUsed = Line.Parent.Parent.Parent:WaitForChild("SwitchBeingUsed", 60)
local Power = Line.Parent.Parent.Parent:WaitForChild("Power")
local PLPower = 1

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
	local PLLevel = Values.PL1Level
	local TweenService = game:GetService("TweenService")

	if BeingUsed.Value == false then
		if Power.Value == 2 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(Switch.Position.X - 1.2, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 3 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(Switch.Position.X - 2.4, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 4 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(Switch.Position.X - 3.6, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		end
	else
		return
	end
end)
```lua
Video

For more detail, I there’s a NumberValue that determines the power. For example, if the Value is set to 1 and you increase the power to 4, it changes to 4.

By the time I am writing this I am offline. Expect to have me be online at 16:00 (4:00 PM) EST

Waiting for a response!

EDIT: That explosion at the start of the video is just a power laser exploding. I didn’t disabled the script so yeah…

local Line = script.Parent
local ClickDetector = Line:WaitForChild("ClickDetector", 60)
local Sound = Line:WaitForChild("SwitchSound", 60)
local Values = workspace:WaitForChild("Values", 60)
local Switch = Line.Parent.Parent.Parent:WaitForChild("Switch")
local BeingUsed = Line.Parent.Parent.Parent:WaitForChild("SwitchBeingUsed", 60)
local Power = Line.Parent.Parent.Parent:WaitForChild("Power")
local PLPower = 1
local OriginalPosX =  Switch.Position.X

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
	local PLLevel = Values.PL1Level
	local TweenService = game:GetService("TweenService")

	if BeingUsed.Value == false then
		if Power.Value == 2 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 1.2, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 3 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 2.4, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 4 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 3.6, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		end
	else
		return
	end
end)

You were trying to switch the position from the current position to back by 2.4 when clicked on 3, so when you wanted it to go from 2 to 3, it would go from 2 to 4.

Also, you should make your script a tad better (not to be rude), consider tables to store information, and work from there to lessen lines from your script.

1 Like

The reason it always moves forwards is because you are adding to the position that is changing every time you add to it instead of setting it to a value

1 doesn’t work at all since there isn’t anything scripted to make it work

local Line = script.Parent
local ClickDetector = Line:WaitForChild("ClickDetector", 60)
local Sound = Line:WaitForChild("SwitchSound", 60)
local Values = workspace:WaitForChild("Values", 60)
local Switch = Line.Parent.Parent.Parent:WaitForChild("Switch")
local DefaultPosition = Switch.Position
local BeingUsed = Line.Parent.Parent.Parent:WaitForChild("SwitchBeingUsed", 60)
local Power = Line.Parent.Parent.Parent:WaitForChild("Power")
local PLPower = 1

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
	local PLLevel = Values.PL1Level
	local TweenService = game:GetService("TweenService")

	if BeingUsed.Value == false then
		BeingUsed.Value = true

		Sound:Play()
		TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(DefaultPosition.X - 1.2 * (Power.Value - 1), Switch.Position.Y, Switch.Position.Z)}):Play()
		wait(2.5)
		PLLevel.Value = PLPower
		wait(0.5)
		BeingUsed.Value = false
	else
		return
	end
end)

This should work

I may try but becuase I am not the best scripter maybe not

Did any of the scripts work in the meantime? If so, mark them as the solution.

1 Like

I’m back. And yes it did work but the problem is that when I try to go back to 1 it basically ain’t working!

Video

New Script
local Line = script.Parent
local ClickDetector = Line:WaitForChild("ClickDetector", 60)
local Sound = Line:WaitForChild("SwitchSound", 60)
local Values = workspace:WaitForChild("Values", 60)
local Switch = Line.Parent.Parent.Parent:WaitForChild("Switch")
local BeingUsed = Line.Parent.Parent.Parent:WaitForChild("SwitchBeingUsed", 60)
local Power = Line.Parent.Parent.Parent:WaitForChild("Power")
local PLPower = 1
local OriginalPosX =  Switch.Position.X

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
	local PLLevel = Values.PL1Level
	local TweenService = game:GetService("TweenService")

	if BeingUsed.Value == false then
		if Power.Value == 2 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 1.2, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 3 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 2.4, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 4 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 3.6, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		end
	else
		return
	end
end)
```lua

That’s because there’s no tween for position 1. Try this:

local Line = script.Parent
local ClickDetector = Line:WaitForChild("ClickDetector", 60)
local Sound = Line:WaitForChild("SwitchSound", 60)
local Values = workspace:WaitForChild("Values", 60)
local Switch = Line.Parent.Parent.Parent:WaitForChild("Switch")
local BeingUsed = Line.Parent.Parent.Parent:WaitForChild("SwitchBeingUsed", 60)
local Power = Line.Parent.Parent.Parent:WaitForChild("Power")
local PLPower = 1
local OriginalPosX =  Switch.Position.X

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
	local PLLevel = Values.PL1Level
	local TweenService = game:GetService("TweenService")

	if BeingUsed.Value == false then
        if Power.Value == 1 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 2 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 1.2, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 3 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 2.4, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		elseif Power.Value == 4 then
			BeingUsed.Value = true
			Sound:Play()
			TweenService:Create(Switch, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(OriginalPosX - 3.6, Switch.Position.Y, Switch.Position.Z)}):Play()
			wait(2.5)
			PLLevel.Value = PLPower
			wait(0.5)
			BeingUsed.Value = false
		end
	else
		return
	end
end)
```lua
1 Like

damn! thanks a lot dude! i really appreciate it!
also i really hate it when it says ```lua instead of actually hiding it

still, thank you so much! but i dont get it why you need that becuase in all other scripts you dont need that but whatever