Event running twice and thrice and fourice


So basically it works for the first time but then the second time it runs two events and the third time it runs three events and so on…
Here’s the script

local mo = game.Players.LocalPlayer:GetMouse()
local cha = game.Players.LocalPlayer.Character
mo.Button1Down:Connect(function()
	local fe = true
	local rotation = 0
	local part = Instance.new("Part",workspace)
	part.Position = Vector3.new(mo.Hit.p.X,mo.Hit.p.Y+part.Size.Y/2,mo.Hit.p.Z)
	mo.Button1Up:Connect(function()
		fe = false
		game.ReplicatedStorage.Events.RemoteEvent:FireServer(CFrame.new(part.Position.X,cha.HumanoidRootPart.Position.Y,part.Position.Z)*CFrame.fromEulerAnglesXYZ(0, math.rad(rotation), 0))
		part:Destroy()
		print(part.Position)
	end)
	repeat 
		local x,y,z = CFrame.new(part.Position,mo.Hit.p):ToOrientation()
		rotation = math.deg(y)
		part.Orientation = Vector3.new(0,rotation,0)
		part.Anchored = true
		part.Transparency = .4
		part.BrickColor = BrickColor.new("Really red")
		wait()
	until fe == false
	
end)

Here’s the serverscript:

game.ReplicatedStorage.Events.RemoteEvent.OnServerEvent:Connect(function(p,e)
p.Character.HumanoidRootPart.CFrame = e
end)
1 Like

So yeah, if you can’t already see it the problem is that the times other than the firsst time you runit, it teleports you back to the position where you ran the first time, which isn’t supposed to happen

Its obvious because you’re connecting a function on Button1Up everytime, on Button1Down event, so it just keeps increasing as you observed. You could just connect it outside, and any variables required could be put at that scope?

No can do. The “Part” variable is only instanced when the button 1 is clicked, and it should forget about it once the event is over.
Also, another thing to note here is that it is actually printing part.Position twice, thrice, and so fourth, indicating that the repeat loop hasn’t been finished in the sense that the part should have been destroyed, but that doesn’t make any sense because if the repeat loop hadn’t been finished, that means it would throw an error saying that part doesn’t exist, because mouse.Button1Up would run anyway if i released my mouse, which it did send the remote event and teleport me once which meant it work, and it also printed out part.Position, which is located a line after part:destroy() indicating that that must HAVE been run, but apparently it didn’t…? How?

I just tried doing repeat part:Destroy() until part==nil and I just got exhausted execution time…
image
then I tried doing repeat part:Destroy() wait() until part==nil, and the repeat loop never ended.

Then I tried printing ‘fer’ every time button1down was done and ‘ahab’ every time the repeat loop for the part orientation thing was run and the output looked normal until you consider that each time i ran there were more print(part.Position)s

Right, then you can simply disconnect the event after it has been used.

local mo = game.Players.LocalPlayer:GetMouse()
local cha = game.Players.LocalPlayer.Character
mo.Button1Down:Connect(function()
	local fe = true
	local rotation = 0
	local part = Instance.new("Part",workspace)
	part.Position = Vector3.new(mo.Hit.p.X,mo.Hit.p.Y+part.Size.Y/2,mo.Hit.p.Z)

    local conn
	conn = mo.Button1Up:Connect(function()
        conn:Disconnect()
		fe = false
		game.ReplicatedStorage.Events.RemoteEvent:FireServer(CFrame.new(part.Position.X,cha.HumanoidRootPart.Position.Y,part.Position.Z)*CFrame.fromEulerAnglesXYZ(0, math.rad(rotation), 0))
		part:Destroy()
		print(part.Position)
	end)

	repeat 
		local x,y,z = CFrame.new(part.Position,mo.Hit.p):ToOrientation()
		rotation = math.deg(y)
		part.Orientation = Vector3.new(0,rotation,0)
		part.Anchored = true
		part.Transparency = .4
		part.BrickColor = BrickColor.new("Really red")
		wait()
	until not fe
end)

Although not sure, what is causing other problems, try after implementing this.

2 Likes

Woah. I’ve ne’er seen :Disconnect() been used or even heard of it. What does it essentially do? And also, your code worked. :white_check_mark:

Basically when you do :Connect on a event, it returns a RBXScriptConnection Object, which essentially has a :Disconnect method, which when run, disconnects that specific connection and doesn’t listen to future events.

1 Like