Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to disable (not script.Disabled) a script once a tool is equipped?

Im trying to temporarily disable a mouse button punching script once the player selects a tool. But when I tried script.Disabled the script still runs. Then I tried adding a bool value to the script, and when the tool is equipped the value will turn false, which should not let the script run. However the script runs regardless. Is there another way?

Edit: Sorry for not putting the script here earlier. Here it is:

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character
local char = player.Character
local mouse = player:GetMouse()
local func = script:WaitForChild("PunchFunc")

local PunchEnable = script.PunchEnable
local enabled = true
local tapped = false

mouse.Button1Down:Connect(function()
	if enabled == true and PunchEnable.Value == true then
		enabled = false
		func:FireServer("Combat", "Left")
		wait()
		enabled = true
	end
end)

mouse.Button2Down:Connect(function()
	if not tapped then 
		tapped = true
		wait(0.3)
		tapped = false
	else
		if enabled == true and PunchEnable.Value == true then
			enabled = false
			func:FireServer("Combat", "Right")
			wait()
			enabled = true
		end
	end
end)

This is a local script that tells the remote "PunchFunc" to fire. Im trying to disable this script while a tool is equipped. PunchEnable is the bool value of the script, and i've already inserted a script into the tool that controls whether this value will be turned false or true depending if its unequipped or equipped. However as previously mentioned it does not work.

1 comment

0

Did my answer help you?

4 answers

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
Character.ChildAdded:Connect(function(NewChild)
   if NewChild:IsA("Tool") then
   --a new tool is equipped -- add disabled script
    --NewChild is the tool equipped
   end
end)
--then to detect if the tool is unequipped you do the opposite
Character.ChildRemoved:Connect(function(RemovedChild)
   if RemovedChild:IsA("Tool") then
   --the tool is unequipped -- add enabled script
   --RemovedChild is the tool unequipped
   end
end)

hope this works, I dont know if it will though

try using

script:Destroy()

2 comments

0

LOL nice

0

You do realize that doesn't stop functions from running right?

The boolean value was a good approach. But you didn't script it to do anything with it so, you would stay with the code that disables that value, and checks on the punch mouse button script before doing anything, so i would assume your code is

local Mouse = game:GetService("Players").LocalPlayer:GetMouse()

if Mouse then

Mouse.Button1Down:Connect(function()
-- code
end)

end

Or something like that, so before running the code check if the value (must be a boolean) inside the script is true, if it isn't then don't do anything

An example would be

local Value = nil -- Define the variable

local Mouse = game:GetService("Players").LocalPlayer:GetMouse()

if Mouse then

Mouse.Button1Down:Connect(function()

if Value.Value == true then

print("The boolean value is true and the player clicked!")

end

end)

end

Since you didn't provide code you would have to do it on your own, but you get the idea.

If this answered your question then mark it as the answer!

4 comments

0

Sorry for not mentioning the script earlier, but I have already tried this and oddly it does not work.

0

Alright, i see. Use the .Unequipped event (from tools) to check whenever it is unequipped and then put the value's value to true. Also make sure it gets replicated.

0

false*

0

.Equipped*

Try connecting your functions to a coroutine like this:

local function punch()
	--Punch function
end
local CoroutineFunction = coroutine.create(punch)
CoroutineFunction.yield()

You can read more about coroutine here

You can call a remote event stored in replicated storage to tell coroutine to yield the function(s). For example:

Server sided:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Yeld Event")

local function Event(Equipped)
	RemoteEvent:FireClient(Equipped) 
end

RemoteEvent.OnServerEvent:Connect(Event)

Client Sided Yield Script:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Yeld Event")

local function Event(Equipped)
	if Equipped then
		--Use coroutine on functions here
	else
		--Use coroutine.resume() here
	end
end

RemoteEvent.OnClientEvent:Connect(Event)

Client Sided Tool Script:

local Tool = script.Parent
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Yeld Event")

local function Equipped()
	RemoteEvent:FireServer(true)
end

local function Unequipped()
	RemoteEvent:FireServer(false)
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

This discussion is preserved as it was. New answers, comments, and votes are closed.