Roblox Anchor Problem SCRIPT

I am currently making the script for the train. So basically, when the driver pressed the key P, the train will be anchor and before the anchor it sets speed to 0. But at this point there is a problem, for the driver script we are using local and for the value change we use script. We use the value to make true and false easier. But when the driver pressed the p key it just changes the button color and train speed doesn’t change and anchor. Any solution or idea is appreciate!

localscript

UIS.InputBegan:Connect(function(key)
	if key.KeyCode.Name == "P" then
		if anchors.Value == false then
			anchors.Value = true
			script.Parent.FramePC.Conducetorssignals.Anchors.TextButton.BackgroundColor3 = Color3.new(0, 1, 0.882353)
		elseif anchors.Value == true then
			anchors.Value = false
			script.Parent.FramePC.Conducetorssignals.Anchors.TextButton.BackgroundColor3 = Color3.new(0, 0.282353, 0.243137)
		end
	end
end)

Script

anchors.Changed:Connect(function()
	if anchors.Value == true then
		wait(0.5)
		script.Parent.Parent.State.currentSpeed.Value = 0
		script.Parent.Parent.State.reqSpeed.Value = 0
		wait(1)
		script.Parent.Parent.Carriage1.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage2.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage3.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage4.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		
		script.Parent.Parent.Carriage1.Collutionbasement.Anchored = true
		script.Parent.Parent.Carriage2.Collutionbasement.Anchored = true
		script.Parent.Parent.Carriage3.Collutionbasement.Anchored = true
		script.Parent.Parent.Carriage4.Collutionbasement.Anchored = true
	elseif anchors.Value == false then
		
		wait(1)
		script.Parent.Parent.Carriage1.Collutionbasement.Anchored = false
		script.Parent.Parent.Carriage2.Collutionbasement.Anchored = false
		script.Parent.Parent.Carriage3.Collutionbasement.Anchored = false
		script.Parent.Parent.Carriage4.Collutionbasement.Anchored = false
	end
end)

Any idea or solution is appreciate! I really need it.

You will have to use a RemoteEvent, when clients edit BoolValues it does not propigate to the server. This means it will look like everything is correct to the client but the server will still think anchors has never changed.

Try using these scripts, with a remote event in replicated storage. The client fires the event when the button is pressed and the server toggles the train on/off.

--- Local Script ---
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

UIS.InputBegan:Connect(function(key)
	if key.KeyCode.Name == "P" then
		if anchors.Value == false then
			script.Parent.FramePC.Conducetorssignals.Anchors.TextButton.BackgroundColor3 = Color3.new(0, 1, 0.882353)
		elseif anchors.Value == true then
			script.Parent.FramePC.Conducetorssignals.Anchors.TextButton.BackgroundColor3 = Color3.new(0, 0.282353, 0.243137)
		end
		remoteEvent:FireServer()
	end
end)
--- Script ---
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
remoteEvent.OnServerEvent:Connect(function()
	if anchors.Value == true then
		wait(0.5)
		script.Parent.Parent.State.currentSpeed.Value = 0
		script.Parent.Parent.State.reqSpeed.Value = 0
		wait(1)
		script.Parent.Parent.Carriage1.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage2.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage3.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		script.Parent.Parent.Carriage4.Main1.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
	end

	task.wait(1)
	script.Parent.Parent.Carriage1.Collutionbasement.Anchored = anchors.Value
	script.Parent.Parent.Carriage2.Collutionbasement.Anchored = anchors.Value
	script.Parent.Parent.Carriage3.Collutionbasement.Anchored = anchors.Value
	script.Parent.Parent.Carriage4.Collutionbasement.Anchored = anchors.Value

	anchors.Value = not anchors.Value
end)
1 Like