Disabling a script works but enabling it doesn't

Hello so i got a shooting script for my gun and i got this line in it:

mouse.Button1Up:Connect(function()
	script.Enabled = false
end)

mouse.Button1Down:Connect(function()
	script.Enabled = true
end)

and enabling it doesn’t work, i know that some of yall may think “well you are trying to enable the script while its disabled!” i tried also enabling it with a different script and it still doesnt work.

This code would never work because it’s inside of the disabled script.
You say that it doesn’t work when you try to enable it from a different script?
Can you show me how you’re enabling it from a different script.

local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	script.Parent.A1.Enabled = true
end)

i also tried using boonleans instead of disabling and enabling the script:

local gun = script.Parent
local empty_sound = game.ReplicatedStorage.clip_empty
local reload_sound = game.ReplicatedStorage.Reload
local player = game.Players.LocalPlayer
local clipSize = gun.Ammo.Value
local ammo = gun.Ammo
local shooting = false
local equipped = false
--UserInputService Setup
local userInput = game:GetService('UserInputService')
--Mouse Icon
local Camera = game:GetService("Workspace").CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
local normalSpeed = 8
local AimingSpeed = 4
--Remote Event Setup
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild('SwitchEvent')
local AimingScript = Character:WaitForChild("AimingScript")
--Checks if Mouse is clicked
gun.Equipped:Connect(function(mouse)
	script.Enabled = true
	player.PlayerGui.ScreenGui.Ammo.Visible = true
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	equipped = true
	shooting = true
	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
			while gun.Ammo.Value > 0 and shooting do 
				shooting = true
				task.wait(0.1)
				remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
				local clone = game:GetService("ReplicatedStorage").BulletModels.TheMainBulletXD:Clone()
				clone.CFrame = gun.Flash.CFrame
				clone.Parent = game.Workspace.BulletFolders
				clone.Orientation = gun.Handle.Orientation
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0.01,math.rad(1),0)
				gun.Flash.PointLight.Enabled = true
				gun.Ammo.Value -= 1
				gun.otherFlash.Transparency = 0
				gun.otherFlash.PointLight.Enabled = true
				wait(0.01)
				gun.otherFlash.Transparency = 1
				gun.otherFlash.PointLight.Enabled = false
				game.Lighting.GunCorrection.Enabled = false
			end
		else
			empty_sound:Play()
		end
		end)
	mouse.Button1Up:Connect(function()
		gun.otherFlash.Transparency = 1
		gun.otherFlash.PointLight.Enabled = false
		game.Lighting.GunCorrection.Enabled = false
		player.PlayerGui.WhiteFlashForGun.Enabled = false
		gun.otherFlash.Transparency = 1
		shooting = false
	end)
	
	mouse.Button1Down:Connect(function()
	end)
	
	mouse.Button2Down:Connect(function()
		if player.Character:FindFirstChildWhichIsA("Humanoid") then
			player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = AimingSpeed


		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 40



	end
		end)



	mouse.Button2Up:Connect(function()
		if player.Character:FindFirstChildWhichIsA("Humanoid") then
			player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = normalSpeed


		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 70



	end



end)



-- Unequip gun



gun.Unequipped:Connect(function()
		gun.otherFlash.Transparency = 1
		gun.otherFlash.PointLight.Enabled = false
		game.Lighting.GunCorrection.Enabled = false
	player.PlayerGui.WhiteFlashForGun.Enabled = false
	gun.Flash.PointLight.Enabled = false
	local camera = game.Workspace.CurrentCamera
	camera.FieldOfView = 70
	player.PlayerGui.ScreenGui.Ammo.Visible = false
	reload_sound:Stop()
	empty_sound:Stop()
end)





--Checks if the letter R is pressed to reload






-- Update ammo GUI



ammo.Changed:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	end)
end)


mouse.Button1Down:Connect(function()
	gun.otherFlash.Transparency = 0
	gun.Flash.PointLight.Enabled = false
	player.PlayerGui.WhiteFlashForGun.Enabled = false
	gun.otherFlash.Transparency = 1
	script.Enabled = true
end)

idk why but it still doesnt work. once i stop shooting i gotta re-equip the gun for it to work.

1 Like

Why did you define another connection for Button1Down:

	mouse.Button1Down:Connect(function()
	end)

Try removing that unnecessary connection and then retry. Please correct me if I’m mistaken or misread your problem.

1 Like

that didnt work sadly. i got no idea what to do tbh.

1 Like

You never break out of the loop once the ammo reaches 0. That could be a leading factor as to why your code isn’t working possibly.

1 Like

how would i break out of the loop? sorry im kind of new to loops.

while shooting do
    if gun.Ammo.Value > 0 then
       gun.Ammo.Value -= 1
    else
        break -- the ammo reached 0 so we break out of the loop
    end
    task.wait(0.1)
end
1 Like

that didnt seem t work too. maybe ill stick to enabling and disabling the script.

Perhaps consider redesigning the system to avoid disabling and enabling the script. It could lead to a cleaner and more efficient architecture in the long run. But do what works for you for now.

bro, im not redesigning nun, i already spent a week trying to make the gun automatic, and another week to fix some bugs.

“But do what works for you for now.” If enabling/disabling works. Stick to it.

first: why you is trying to disable the script? to stop it from working? and what is the point of doing this?

so basically, i have this script, and whenever i stop shooting i gotta re-equip the gun for it to work again, so im trying to make it so whenever i stop shooting the script is disabled, and when i start shooting the script is enabled, the problem is that it doesnt work.

You’re also setting shooting to true when they literally equip the weapon before even firing? Seems a bit off, I would personally put that within the Button1Down connection function.

it worked bro, thank you so much for your advice.

why everything its inside of gun.Equipped? every time the player equip the gun everything inside of this connection will execute

bro im not the best scripter, i didn’t know that.

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