How can I fix this script?

  1. What do I want to achieve?

The first time the tool is clicked (while being equipped) I want it to fetch the part they clicked on (THAT WORKS!), then I want it to make the textlabel above it say "Connect second part." (THAT WORKS!), the second time a different part is clicked, I want it to start the connecting process (as shown in the script) but instead all it does is show “Connect second part” when I click the second part

  1. What is the issue? Include screenshots / videos if possible!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to get help from the unofficial discord server, but they couldn’t find out what.

local UIS = game:GetService("UserInputService")

script.Parent.Equipped:Connect(function()
	local plr = script.Parent.Parent.Name
	print("Hammer equipped by " .. plr)
	for _, child in pairs(workspace:GetDescendants()) do
		if child:IsA("BasePart") and child.Name == "part" then
			if child.Editable.Value == true then
				child.GUI.CT.Visible = true
			end
		end
	end
end)

script.Parent.Unequipped:Connect(function()
	print("Hammer unequipped")
	for _, child in pairs(workspace:GetDescendants()) do
		if child:IsA("BasePart") and child.Name == "part" then
			if child.Editable.Value == true then
				child.GUI.CT.Visible = false
			end
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end

	local plr = script.Parent.Parent.Name

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mouse = UIS:GetMouseLocation()
		local ray = game.Workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)

		local hit = workspace:Raycast(ray.Origin, ray.Direction * 100)

		if hit and hit.Instance:IsA("BasePart") and hit.Instance.Name == "part" then
			if hit.Instance.Editable.Value == true then
				local firstpart = "df"
				local secondpart = "df"
				local lastlclick = "df"
				if firstpart == "df" then
					firstpart = hit.Position
					hit.Instance.GUI.CT.Text = "Connect second part."
					lastlclick = hit.Instance.Name
					print(firstpart)
					print(secondpart)
				elseif secondpart == "df" and firstpart ~= "df" and hit.Position ~= firstpart then
					secondpart = hit.Instance.Name
					hit.Instance.GUI.CT.Text = "Connecting 1/2"
					lastlclick.GUI.CT.Text = "Connecting 1/2"
					task.wait(2)
					hit.Instance.GUI.CT.Text = "Connecting 2/2"
					lastlclick.GUI.CT.Text = "Connected 2/2"	
					task.wait(1)
					hit.Instance.GUI.CT.Text = "Connected"
					lastlclick.GUI.CT.Text = "Connected"	
					print(firstpart)
					print(secondpart)
				end
			end
		end
	end
end)

I’m more new to scripting, and thanks for the help to the people who are whilling to help me!

1 Like

Is that script a Local Script or ServerScript?

You reset firstpart secondpart and lastlclick each time the player clicks. You probably need to move those three lines outside of the function:

local firstpart = "df"
local secondpart = "df"
local lastlclick = "df"

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    ...

You should also use nil to mean none, if that’s what you’re using “df” to mean.

The script is a localscript inside the part.

Also since you have waits in your input function, you will eventually need to make sure to “lock” the function using another value while its in the process of connecting. Otherwise if you click too fast you will get crazy side effects.

Yea, I do plan to improve it soon.

Good news, This fixed the problem, KINDA, now I’m having issues with it just auto connecting instead of waiting for a second input/click

You probably also have to reset those three values to where they started after you finish connecting, so at the end of the last elseif

What do you mean, I’m not the best at scripting…

Like this:

...
elseif secondpart == "df" and firstpart ~= "df" and hit.Position ~= firstpart then
	secondpart = hit.Instance.Name
	hit.Instance.GUI.CT.Text = "Connecting 1/2"
	lastlclick.GUI.CT.Text = "Connecting 1/2"
	task.wait(2)
	hit.Instance.GUI.CT.Text = "Connecting 2/2"
	lastlclick.GUI.CT.Text = "Connected 2/2"	
	task.wait(1)
	hit.Instance.GUI.CT.Text = "Connected"
	lastlclick.GUI.CT.Text = "Connected"	
	print(firstpart)
	print(secondpart)

	--add this
	firstpart = "df"
	secondpart = "df"
	lastlclick = "df"
end
...

Okay, this kinda fixes the issue, now I get:

Players.Dacatboy_YT.Backpack.Hammer.MainHandling:54: attempt to index nil with 'CT'

and it doesn’t change the text of the first part that was clicked

Have you tried stepping through it and making sure its taking the path you expect?

It is fixed, Thank you, you are amazing!

1 Like

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