so ive been making a game where i need to make a pipe snapping system. these 2 scripts are the 2 im using for it. 1 of tehm is to toggle building and tell the server when the player clicks, the other is supposed to save the position and check if there is an attachment point nearby. The server script isnt working and i even have tons of print(...) in it for debugging (the ... is just whatever event is happening). So how can i fix it? client script:

Players = game:GetService("Players")
LocalPlayer = Players.LocalPlayer
Mouse = LocalPlayer:GetMouse()
RE = game.ReplicatedStorage.RE
reps = 0
local function toggle(Enabled)
	if Enabled then
		Enabled = false
	else
		Enabled = true
	end
	return Enabled
end

script.Parent.MouseButton1Down:Connect(function(clicked)
	print("triggered first click")
	local Enabled = toggle(script.Parent.Enabled.Value)
	script.Parent.Enabled.Value = Enabled
	if Enabled then
		print("enabled is true")
		wait(Mouse.Button1Up)
		print("Mouse Button is up")
		Mouse.Button1Down:Connect(function(click)
			local Mousepos = Vector3.new(Mouse.hit)
			RE:FireServer(Mousepos)
			print("Fired server event")
			if reps >= 2 then
				print("reset reps")
				reps = 0
				Enabled = false
			end
		end)
	end
end)

server script:

local pos1 = nil
local pos2 = nil

local function triggered(Player, Mousepos)
	print("triggered")
	local PipeChecker = game.ServerStorage.PipeConnectionChecker:Clone()
	PipeChecker.Name = "PipeChecker"
	PipeChecker.Parent = workspace
	PipeChecker.Position = Vector3.new(Mousepos)
	PipeChecker.touched:Connect(function(hit)
		print("touched")
		if hit and hit.Name then
			if hit.Name == "PipeAttachment" then
				print("Touched attachment point")
				if pos1 == nil then
					pos1 = hit.Position
				else
					if pos2 == nil then
						pos2 = hit.Position
					else
						pos1 = hit.Position
						pos2 = nil
					end
				end
			end
		end
	end)
	PipeChecker:Destroy()
	print("destroyed checker")
	print(pos1, pos2)
end

game.ReplicatedStorage.RE.OnServerEvent:Connec(triggered)

1 comment

0

also every time i toggle on the client script it fires the server event again. By that i mean toggling it then clicking wherever will make it trigger an extra time.