I dont know how to stop function

this is my scope in script,but it has a problem , the cframe part with the arms wont stop after the gun is aimed and i dont know how to cancel it out by putting a line of code after the tween 2 play line

if firstPerson and config:WaitForChild("Ironsight").Value then
				animationTrack:Play()
				
				local scope = script.Parent.Scope
				scope["Meshes/sniper_scope"].Transparency= 0.1
				scope.Part.Transparency = 0.97
				
				scope.Part19.Transparency = 0.9
				scope.Part19.Reflectance = 0.4
				
				scope.Part.Reflectance = 0.4
				
				
			
				local player = game.Players.LocalPlayer
				local character = player.Character
				if not character or not character.Parent then
					character = player.CharacterAdded:wait()
				end
				local humanoid = character:WaitForChild("Humanoid")
				local torso = character:WaitForChild("Torso")
				local rightShoulder = torso:WaitForChild("Right Shoulder")
				local leftShoulder = torso:WaitForChild("Left Shoulder")
				local camera = game.Workspace.CurrentCamera

				updateSpeed = 1

				game:GetService("RunService").RenderStepped:Connect(function()
					--character["Right Arm"].LocalTransparencyModifier = character["Right Arm"].Transparency
					--character["Left Arm"].LocalTransparencyModifier = character["Left Arm"].Transparency
					local camCF = camera.CoordinateFrame
					local distance = (character.Head.Position - camCF.p).magnitude
					if distance <= 1.01 and humanoid.Health ~= 0 and input.UserInputType == Enum.UserInputType.MouseButton2 then
						rightShoulder.C0 = rightShoulder.C0:lerp((camCF * CFrame.new(1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * CFrame.Angles(0, math.pi/2, 0), updateSpeed)
						leftShoulder.C0 = leftShoulder.C0:lerp((camCF * CFrame.new(-1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * CFrame.Angles(0, -math.pi/2, 0), updateSpeed)
					else
						rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0)
						leftShoulder.C0 = CFrame.new(-1, 0, 0) * CFrame.Angles(0, -math.pi/2, 0)
					end
				end)
			
				--zone
				
				tween1:Play()
				if config:WaitForChild("IronsightWalkSpeedReduce").Value then
					humanoid.WalkSpeed = config:WaitForChild("IronsightWalkSpeed").Value
				end
			end
		end
		
	end)
	
	UserInputService.InputEnded:Connect(function(input, gameProccesed)
		if not gameProccesed or input.UserInputType == Enum.UserInputType.MouseButton2 then
			tween2:Play()
			--SOLUTION CODE HERE
			animationTrack:Stop()
			local scope = script.Parent.Scope
			scope["Meshes/sniper_scope"].Transparency= 0
			scope.Part.Transparency = 0
			scope.Part19.Transparency = 0
			scope.Part19.Reflectance = 1
			scope.Part.Reflectance = 1
			
			
			if config:WaitForChild("IronsightWalkSpeedReduce").Value then
				humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
				
				
			end
			
		end
	end)
1 Like

:Connect() returns an object that you call :Disconnect() on to stop it. You have a RenderStepped loop that needs disconnected this way.

1 Like

Anyway as i was trying to say,

function InputStarted(Input)
–your code here
end)
function InputEnded(Input)
–your code here
end)
UIS.InputBegan:Connect(InputStarted)
UIS.InputEnded:Connect(InputEnded)

this will call the corresponding function when the input start/ends. For example

function InputStarted(Input)
print(“Start”)
end)
function InputEnded(Input)
print(“End”)
end)
UIS.InputBegan:Connect(InputStarted)
UIS.InputEnded:Connect(InputEnded)

In this code when a button is pressed it will print “Start” and when you stop pressing the button it will print “End”.

1 Like
local RunS = game:GetService('RunService')

function InputStarted(Input)
 --your stuff
   RunS:BindToRenderStep('Aim', 1, function()
      --your other stuff
   end)
end)

function InputEnded(Input)
   --your stuff
   RunS:UnbindFromRenderStep('Aim')
end)

UIS.InputBegan:Connect(InputStarted)
UIS.InputEnded:Connect(InputEnded)

Alternatively:

local RunS = game:GetService('RunService')
local connection

function InputStarted(Input)
 --your stuff
   connection = RunS.RenderStepped:Connect(function()
      --your other stuff
   end)
end)

function InputEnded(Input)
   --your stuff
   connection:Disconnect()
end)

UIS.InputBegan:Connect(InputStarted)
UIS.InputEnded:Connect(InputEnded)

thank you so much , i finally did it

but i have a question , it doesnt work in all tools , in my gun1 it works but in my gun2 in breaks , even though both guns give the error atempt to index nil with ‘Disconnect’ - Client - LocalScript:93

Dont forget to mark it as solution

i dont want to mark it as a solution

bro he just wasted his time on your topic and you won’t mark it as solution

1 Like

my gun is still half broken and i still have no solutions to fix it

i want the topic to be open so i can find an answer

If there’s updated code, mind posting? Otherwise impossible (to me at least) to predict what the issue is

that is the code

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("Torso")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local leftShoulder = torso:WaitForChild("Left Shoulder")
local RunS = game:GetService("RunService")
local connection
local TweenService = game:GetService("TweenService")
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local config = tool:WaitForChild("Configuration")
local remotes = tool:WaitForChild("Remotes")
local gunCursorIcon = "rbxasset://textures\\GunCursor.png"
local normalIcon = ""
local camera = workspace.CurrentCamera
local head = player.Character:WaitForChild("Head")
local animation = tool:WaitForChild("Animations"):WaitForChild("Aim")
local cameraTweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)
local function isFirstPerson()
	if (head.CFrame.p - camera.CFrame.p).Magnitude < 1.01 then
		print(head)
		return true
	else
		return false
	end
end
local goal1 = {FieldOfView = config:WaitForChild("IronsightFov").Value}
local goal2 = {FieldOfView = 70}

local tween1 = TweenService:Create(camera, cameraTweenInfo, goal1)
local tween2 = TweenService:Create(camera, cameraTweenInfo, goal2)
local animationTrack = humanoid:LoadAnimation(animation)

UserInputService.InputBegan:Connect(function(input, gameProccesed)
	if not gameProccesed and input.UserInputType == Enum.UserInputType.MouseButton2 then
		local firstPerson = isFirstPerson()



		if firstPerson and config:WaitForChild("Ironsight").Value then
			animationTrack:Play()

			local scope = script.Parent.Scope
			scope["Meshes/sniper_scope"].Transparency= 0.1
			scope.Part.Transparency = 0.97
			script.Parent.Model.Iron.Transparency=0.97
			script.Parent.Model.Iron2.Transparency=0.97
			scope.Part19.Transparency = 0.9
			scope.Part19.Reflectance = 0.4

			scope.Part.Reflectance = 0.4



			
			local camera = game.Workspace.CurrentCamera

			updateSpeed = 1

			connection = RunS.RenderStepped:Connect(function()
				--character["Right Arm"].LocalTransparencyModifier = character["Right Arm"].Transparency
				--character["Left Arm"].LocalTransparencyModifier = character["Left Arm"].Transparency
				local camCF = camera.CoordinateFrame
				local distance = (character.Head.Position - camCF.p).magnitude
				if distance <= 1.01 and humanoid.Health ~= 0 and input.UserInputType == Enum.UserInputType.MouseButton2 then
					rightShoulder.C0 = rightShoulder.C0:lerp((camCF * CFrame.new(1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * CFrame.Angles(0, math.pi/2, 0), updateSpeed)
					leftShoulder.C0 = leftShoulder.C0:lerp((camCF * CFrame.new(-1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * CFrame.Angles(0, -math.pi/2, 0), updateSpeed)
				
					

				end
			end)

			--zone

			tween1:Play()
			if config:WaitForChild("IronsightWalkSpeedReduce").Value then
				humanoid.WalkSpeed = config:WaitForChild("IronsightWalkSpeed").Value
			end
		end
	end

end)

UserInputService.InputEnded:Connect(function(input, gameProccesed)
	if not gameProccesed or input.UserInputType == Enum.UserInputType.MouseButton2 then
		tween2:Play()
		connection:Disconnect()
		rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0)
		leftShoulder.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0)
		animationTrack:Stop()
		local scope = script.Parent.Scope
		scope["Meshes/sniper_scope"].Transparency= 0
		scope.Part.Transparency = 0
		script.Parent.Model.Iron.Transparency=0
		script.Parent.Model.Iron2.Transparency=0
		scope.Part19.Transparency = 0
		scope.Part19.Reflectance = 1
		scope.Part.Reflectance = 1


		if config:WaitForChild("IronsightWalkSpeedReduce").Value then
			humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed


		end

	end
end)

lol doesnt matter i figured it out myself and i made it up and running but i wouldnt have mande it without him

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