Script breaks after I clone model and set the script.Disabled to false

Hello fellow developers,
I have been working on a tripwire tool for my game and how it works is that you click with the tool to place a cloned model with a peg and click again to place the second peg after that I disable the script inside the model to activate the code that connects the string:

local part = script.Parent.Parent.WallPart
local A = script.Parent.PrimaryPart
local B = script.Parent.PrimaryPart
local RunService = game:GetService('RunService')
RunService.Heartbeat:Connect(function()
	local magnitude = (A.Position - B.Position).magnitude
	part.Size = Vector3.new(part.Size.X, part.Size.Y, magnitude)
	part.Anchored = true
	part.CFrame = CFrame.new(
		A.Position:Lerp(B.Position, 0.5),
		B.Position
	)
end)

Problem: Everything works until I click to place the second peg and then the script is not disabled but the script does not do it’s function ^. And i have checked it’s not the scripts problem which it isn’t.
Here is the code in the tool:

db = true
local A
local B
local TP
local made = false
script.Parent.Activated:Connect(function()
	if made == false then
		TP = workspace.Tripwire:Clone()
		TP.Parent = workspace
		A = TP.A
		B = TP.B
	end
	local char = script.Parent.Parent
	local offset = Vector3.new(0,-2.5, -3)
	if db == true then
		db = false
		made = true
		A.PrimaryPart.CFrame = char.Torso.CFrame * CFrame.new(offset)
	elseif db == false then
		db = true
		B.PrimaryPart.CFrame = char.Torso.CFrame * CFrame.new(offset)
		made = false
		A.TripwireMaker.Disabled = false
	end
end)

Here is a snippet of what happens (note: a potential clue i found was that the WallPart’s position was (0,-1000000,0) That maybe why??):

This is what should happen when i place the pegs:


Sorry if it is messy, I am still learning and improving.

1 Like

I Havenot seen an issue within your script when i used almost the same functions, but if u still cant solve ur issue try adding print(“”) under every function to find out where the issue starts at like for example

script.Parent.Activated:Connect(function()
        print("1")
	if made == false then
        print("2")
		TP = workspace.Tripwire:Clone()
		TP.Parent = workspace
		A = TP.A
		B = TP.B
	end
	local char = script.Parent.Parent
	local offset = Vector3.new(0,-2.5, -3)
	if db == true then
        print("3")
		db = false
		made = true
		A.PrimaryPart.CFrame = char.Torso.CFrame * CFrame.new(offset)
	elseif db == false then
        print("4")
		db = true
		B.PrimaryPart.CFrame = char.Torso.CFrame * CFrame.new(offset)
		made = false
		A.TripwireMaker.Disabled = false
	end
end)
1 Like

From what I am aware, setting a script that is already running to Disabled will not terminate the thread. If the thread is allowed to reach a logical conclusion, ie not further statements after you have done the placement and Enabled the touch detection script, then it will end.

I think you will need to Disconnect then function to allow it to terminate gracefully.

5 Likes

Thank you for replying, I will try both ideas thank you.

2 Likes

The print statement showed that the script runs and everything but the WallPart is still teleported to (0,-100000000000,0)

1 Like

Amazing! @BadDad2004 Your idea worked and now i can continue making the tripwire thank you!

1 Like

The funny thing is that API Reference states that:

  • “If Disabled is set to true whilst a script is running, the current thread will be terminated.”

Which is actually the opposite on what its supposed to do, cause the current thread would be still running even if you set its Disabled property to true

I suppose it only works if you want to disable certain functions from preventing it running again

Don’t know if anyone has mentioned it in the #bug-reports:developer-hub category though

2 Likes

I think it has been brought up multiple times in multiple threads on the DevForum, so I am surprised they are not aware that the documentation is wrong. Unless of course it is a bug and we should really be requesting a bug fix?

2 Likes