Door welding issues

So, I’m using NWSPACEK’s script, for welding my trains to be able to drive, it basically:

  • Is placed inside the train model (Named “Advanced Weld”)
    Photo of the train carriage model, note is does continue going down with parts and other models etc:
    image

  • It will weld everything and then unanchor everything, except the coupler as its important for the functionality, (the train uses a drive seat and sets of wheels for it to work)

  • However the door script I have made doesn’t like the welding, and the door will always fall off the main train model, the carriage moves fine and the door works, but its not attached to the carriage.
    Photo:

  • What I’m asking is if I can have any support, I’ve tried ChatGPT with no success, and I would really love to find a solution, I have attached both codes below

The main welding script:

--MADE BY NWSPACEK

--[[ This weld script is cool!
	It will use the structure of models to weld. It treats each model as a separate moving body
	except if name a part "AHandle" and insert a BoolValue named "WeldToRoot" into it.
--]]

wait(1) --thanks PSG

local AvoidWelding = {--add models you want this script to not weld in here
	["Coupler"]=true --Ignores the coupler model
}

function weld(i,b)
	local weld1 = Instance.new("Weld") 
	weld1.Name = b.Name
	weld1.Part0 = i
	weld1.Part1 = b
	weld1.C1 = b.CFrame:inverse()*i.CFrame
	weld1.Parent = i
end

function welding(p,ph)
	p:MakeJoints()
	local handle = p:FindFirstChild("AHandle")
	if handle and ph and handle:FindFirstChild("WeldToRoot") then
		weld(handle,ph)
	end
	if not handle then
		for _,j in pairs (p:GetChildren()) do
			if j:IsA("BasePart") then
				handle = j
				break
			end
		end
	end
	if not handle then
		handle = ph
	end
	for _,j in pairs (p:GetChildren()) do
		if j:IsA("BasePart") then
			weld(handle,j)
		elseif j:IsA("Model") and not AvoidWelding[j.Name] then
			welding(j,handle)
		end
	end
end

function unanchoring(p)
	for _,j in pairs (p:GetChildren()) do
		if j:IsA("BasePart") then
			j.Anchored = false
		elseif j:IsA("Model") then
			unanchoring(j)
		end
	end
end

wait(1)
welding(script.Parent)
wait(0.5)
unanchoring(script.Parent)

The Door script:

local frame = script.Parent
local openSound = frame:WaitForChild("DoorOpen")
local closeSound = frame:WaitForChild("DoorClose")
local clickDetector = frame:WaitForChild("ClickDetector")
local model = frame.Parent
local frameClose = model:WaitForChild("DoorFrameClose")
local frameOpen = model:WaitForChild("DoorFrameOpen")
local opened = model:WaitForChild("Opened")
local tweenService = game:GetService("TweenService")

local debounce = true
clickDetector.MouseClick:Connect(function()
	if debounce == true then
		debounce = false
		if opened.Value == true then
			opened.Value = false
			
			closeSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
		else
			opened.Value = true
			
			openSound:Play()
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameOpen.CFrame}):Play()
		end
		
		wait(0.35)
		debounce = true
	end
end)
2 Likes

You have the carriage model with the weld script.
The door should be manually welded, or have its own weld script inside it, and be in the AvoidWelding function table. If it’s in the carriage the weld script will try to weld the door parts to the carriage parts.
You now need to add a WeldConstraint between the door and the carriage for the tween script to work with. Make sure the weld is Enabled and has the C0 and C1 properties filled in.
Your tween script is designed for Anchored Parts.
Look up scripts for tweening the weld so the door can move with the train.

2 Likes

Thank you very much for your reply, I really appreciate it and hopefully it will fix it!
However, do you have any examples of scripts which tween the weld? I’m struggling to find any.

Edit:
I found a script @Scottifly (sorry for ping) , but I’m not sure whether I have set it up properly, the door was welded on and functioned as intended, however now the train will not move.

1 Like

Is anything Anchored now when you test it?
Is there a weld between the carriage and anything else?

This is kind of why I like using WeldConstraints instead. Sure it takes a bit of work, but it’s so much easier to visualize with the ‘Show Welds’ tool in the Constraints tab in Studio.

1 Like

I’m making progress, the train carriage now moves, however the door doesn’t open now.
Any ideas on how to rectify this, as now I have reached a big step in progress it would be really annoying to loose it all. The weld constraint is the problem for the door now, upon disabling it the door falls but plays the animation of it opening, however when its enabled the door doesn’t move at all.

Oh and all welds are internal to the carriage only, so even the coupling system works fine!

2 Likes

Ah, don’t change the CFrame of the door since it’s not Anchored. Change the CFrame of the Weld C0 or C1.

1 Like

Sorry? I don’t quite understand what you mean, you want me to change the 0.35 to 0? In the door script there is no C1 or C0 I don’t think, unless I’m being really stupid.

2 Likes

You can tween the CFrame of the weld, you just have to reference the C0 or C1. For example in your open tween:
{C0 = CFrame.new(0,0,0)}
And in your close tween:
{C0 = CFrame.new(4,0,0)}

It might be C1 instead or (0,0,5) or whatever values work for your door.

1 Like

I still don’t follow you, I’m really sorry.
Could you make it a bit simpler?

1 Like

In your original script you are using the goal {CFrame = frameClose.CFrame} but this is trying to change the Position of the door. It won’t work because the door isn’t Anchored.
If you CFrame the C0 or C1 (for example I used a goal of {C0 = CFrame.new(4,0,0)} to change the distance the weld between the door and the carriage tweens.

1 Like

So I replace the {CFrame = frameClose.CFrame} with {C0 = CFrame.new(4,0,0)}?

1 Like

local frameClose = model:WaitForChild("DoorFrameClose")

Not exactly. You see how you have frameClose as the reference in your tween line? You now have to reference the Weld instead so that when you call the C0 part of my script it’ll work.

For instance, in the script I used my variable for the weld is like this:
door1 = script.Parent.Door1.Door1.Weld
which references the Weld.

My tween properties are set like this because it’s easier just to set them one time:

tweenInfo = TweenInfo.new(
		3,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut			
		)

Then I set the variables tweenOpen1 and tweenClosed1 like this:
tweenOpen1 = tweenService:Create(door1, tweenInfo, {C0 = CFrame.new(0,0,0)}) tweenClose1 = tweenService:Create(door1, tweenInfo, {C0 = CFrame.new(1.39,0,0)})
You can see I’ve only changed the X property of the CFrame, but yours may be different, with different values.
This also helps clean up your code if you want to run the tween from different places because again, it only needs to be set once. You may not want to do this if you have multiple doors that you want to run off the same script though.

Then when I want the open tween to run in my script I use this:
tweenOpen1:Play()

And to close it:
tweenClose1:Play()

1 Like

The script I use basically just makes the door move towards DoorFrameClose, and then when opened it will toggle to DoorFrameOpen, previously I was using weld constraints.

I appreciate this, as its got to be individual doors being opened, and I now understand what you meant by C0 and C1 Variables.
However, I’m not sure what to weld the door to, as whenever I weld it to something, the door will snap to that and doesn’t work at all then, you got any ideas how to rectify this?

  • Do bear in mind though that the DoorFrameOpen and DoorFrameClose must be inside the door model, which is an exception to the main welding process.
1 Like

You don’t need the variables model, frameOpen and frameClose at all with my script. Those are just leftovers from your script that tell the door where the open and closed positions should be, but since your script was for Anchored Parts they are not useful. All you need with my script is a weld that’s set up properly.

With your weld you need to set the C0 and C1 Offset values to put the door where you originally want it in realation to the other Part. Usually only one of them is offset, the other is just left at 0,0,0 (the center of the Part that’s being welded.

The weld is easy to set up in Studio. Change one of the 3 values in the CFrame to set where the door goes to in the correct open Position.
For example let’s say your weld C0 Offset is going to be (5.2, 2, 3.9)
When you’ve got that sorted out, see what the value is when you set the weld to the closed Position.
Let’s say (5.2, 2, 9.9) if you need the door to open 6 studs in the Weld’s Y Position.
Use that difference to set your value in the lines I gave you:

tweenOpen1 = tweenService:Create(door1, tweenInfo, {C0 = CFrame.new(0,0,0)}) -- the original open Position
tweenClose1 = tweenService:Create(door1, tweenInfo, {C0 = CFrame.new(0,0,6)}) -- offsets the weld Y by 6 studs

When you play tweenOpen1 it’ll CFrame the weld C0 to it’s original Position. Playing tweenClose1 will shift the door 6 studs to the Closed Position.

1 Like

Do you know if you could help me out in studio regarding this?
Since I’m very new to Cframing and it would be amazing to have a bit of help.

1 Like

That’s the beauty of this, you don’t need to know exactly how to CFrame. The CFrame part of that line only changes the Weld’s offset.
I just gave you everything you need.
Remove all the extra lines I mentioned.
Add the sections of code I gave you.
Go to the main Part of your door and add a Weld to it by right clicking, click Insert Object, and then click Weld.
Select the Weld in the door Part. Click the Part0 property then select the door main Part.
Click the Part1 property then select the Part of the carriage you want the door welded to (doesn’t matter which part really, but if you use the same Part (like a doorframe Part) for all the doors in each carriage it will probably be easier to set the Weld Offsets.
The door will snap the centers of each Part together. Now type numbers into the Position of the C1 x, y and z. Change the offset the weld to get the door aligned to where you want it in the open Position.
Now change the numbers of the C0 Offset so the door moves to where you want it in the closed Position. This tells you what values to put in the code for the weld CFrame so the door tweens from open (0,0,0) to whatever you need it for the open Position.
This line:
door1 = script.Parent.Door1.Door1.Weld
gives you the information about the door1 variable referencing the weld. Make sure the script.Parent... actually points to the Weld in your main door Part.
Now in the tweenClose1 = tweenService:Create(door1, tweenInfo, {C0 = CFrame.new(0,0,6)}) line that I gave you change (0,0,6) to whatever your C0 closed position should be from the above steps.

When you want to open the doors, call the tweenOpen1. When you want to close them call tweenClose1.

1 Like

Thank you very much for all the help, I really appreciate it!

Do you know if in the future you could help me with welds? It seems your really good at it and I would love to practice sometime.

1 Like

You can just ask your question on these forums.
If I know then I can help out, but there is a lot I don’t know so others may be able to help you out.

2 Likes

Thanks once again for all the help, have an amazing day/night!

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