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

How do I switch a piece of track using an outside source?

Hello! I have an issue. I am trying to sync my railroad depot control signals with a switch, but it seems as if there is no way to do that. I'm confused a bit.

function onClicked()
	if script.Parent.Parent.Parent.IsOn.Value == false then
    script.Parent.Parent.Parent.IsOn.Value = true
 	script.Parent.Parent.Switch2.Transparency = 1
	script.Parent.Parent.Switch2a.Transparency = 1
	script.Parent.Parent.Switch2b.Transparency = 1
	script.Parent.Parent.Switch1.Transparency = 0
	script.Parent.Parent.Switch1a.Transparency = 0
	script.Parent.Parent.Switch1b.Transparency = 0
	elseif script.Parent.Parent.Parent.IsOn.Value == true then
    script.Parent.Parent.Parent.IsOn.Value = false
	script.Parent.Parent.Switch2.Transparency = 0
	script.Parent.Parent.Switch2a.Transparency = 0
	script.Parent.Parent.Switch2b.Transparency = 0
	script.Parent.Parent.Switch1.Transparency = 1
	script.Parent.Parent.Switch1a.Transparency = 1
	script.Parent.Parent.Switch1b.Transparency = 1
	
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Thats for the outside source which controls the IsOn value for the ControlBox model which activates the lights and does its stuff. Here is the script for the track switch (independent of the outside source)

Here is the script for the track switch itself:

local Open = true
local Trans = {[true] = 0, [false] = 0.8}
wait(0.1)

function SetState(state)
	for _, p in pairs(script.Parent:GetChildren()) do
		if p.Name == "Open" then
			p.CanCollide = state
			p.Transparency = Trans[state]		
		elseif p.Name == "Close" then
			p.CanCollide = not state
			p.Transparency = Trans[not state]
		end
	end
	Open = state
end

script.Parent.OpenBlock.Touched:connect(function(p)
	SetState(true)
end)

script.Parent.CloseBlock.Touched:connect(function(p)
	SetState(false)
end)

script.Parent.Switch.Click.MouseClick:connect(function()
	SetState(not Open)
end)

SetState(true)

Any help is appreciated. Thanks!

3 comments

0

Try using lua globals.

0

Trying that right now

0

Didn't work-maybe im not using it correctly

1 answer

Changed some things around and added an elseif to the script.

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