Script disabled when tool is held

I’m trying to make it so that my punch script gets disabled when a tool is held, but for some reason I can’t get the script to be disabled.

I’ve tried looking at the script from this link: Trying to Disable a script when tool is equipped [SOLVED]
But that doesn’t solve my issue.

Script is

local PunchScript = game.Players.LocalPlayer.PlayerGui.Main.PunchScript

script.Parent.Equipped:Connect(function()
	PunchScript.Enabled = false
end)

script.Parent.Unequipped:Connect(function()
	PunchScript.Enabled = true
end)
2 Likes

From testing, it seems that tool.Equipped only works in a server script, not local script, so you’ll have to put that code in a server script

1 Like

The script is a server script that I put under the tool.

In which case local player won’t work.

1 Like

Not entirely sure where you’re getting that information from. tool.Equipped works in both Server and Local scripts. I think the issue here is that if the script in question here is a LocalScript, disabling a script will not work, as any changes a client makes to anything outside of the character directly will not replicate to the server, and thus, will have no effect.
If you want to disable the script from a LocalScript, you will have to tie it to a RemoteEvent, picked up by a separate ServerScript in the tool which will then disable/enable the script in question when the events are handled. For example.

--[[
Layout of tool:
Tool
    Events - Folder
        DisableEvent - RemoteEvent
    PunchScript
    DisableClient - LocalScript
    DisableServer - ServerScript
]]--

-- DisableClient
local Tool = script.Parent
local Events = Tool.Events
local DisableEvent = Events.DisableEvent

Tool.Equipped:Connect(function()
    DisableEvent:FireServer(true)
end)

Tool.Unequipped:Connect(function()
    DisableEvent:FireServer(false)
end)

-- DisableServer
local Tool = script.Parent
local Events = Tool.Events
local DisableEvent = Events.DisableEvent
local PunchScript = Tool.PunchScript

DisableEvent.OnServerEvent:Connect(function(Player, Value)
    PunchScript.Enabled = Value
end)

Not sure if I’ve missed anything here, hopefully not, but this should be the solution to your problem :+1:

1 Like

Would I have to do something with the disable event in the script i’m trying to disable? Like how you did

1 Like

No, you would only need a LocalScript and a ServerScript to interact with eachother with a RemoteEvent, with the ServerScript disabling the script you don’t want to be enabled. The actual script itself, in your case, the PunchScript, would stay exactly the way it is. If you look at where I put “Layout of the tool”

--[[
Layout of tool:
Tool
    Events - Folder
        DisableEvent - RemoteEvent
    PunchScript
    DisableClient - LocalScript
    DisableServer - ServerScript
]]--

You can see the layout of the Tool itself. The spaces indicate it’s a child of the Tool, the dash indicates what that object actually is. Everything in my response is labelled and should be relatively easy to follow. You can literally just copy+paste the code I gave and organise your tool to look like that and it should (hopefully) work. Let me know if anything goes wrong.

Your script is a server script inside of the tool, no?
While the person above me solution while work, its way too complicated.
Just add a new server script in your code, delete the local script one, and paste this in:

-- local player cant be used in server scripts
local PunchScript = script.Parent.Parent.Parent.PlayerGui.Main.PunchScript

script.Parent.Equipped:Connect(function()
	PunchScript.Enabled = false
end)

script.Parent.Unequipped:Connect(function()
	PunchScript.Enabled = true
end)

Had to switch out the trye and false right here, it was making it so that the script work when the tool was held and didnt work when it wasnt being held

now the problem is that the tool doesnt work at all

Yes, it is a server script. When I tried the script you told me it worked the same as the original script

Oh right, apologies for that, got my logic a bit mixed up. Glad to hear it works though :+1:

It kinda does work, but for some reason disables any tool that I put it in

That’s odd. Could just be the way you’re putting it in. Mind sending over a screenshot of the layout of your tool if you don’t mind?

Screenshot 2023-08-03 155456
Here’s the layout of the tool. It is a free model im using before I make my own actual tool.

First, we can simplify this slightly.
We don’t need two functions, we can use one and then find out which.

local PunchScript = game.Players.LocalPlayer.PlayerGui.Main.PunchScript

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
    if script.Parent.Parent:IsA("Model") then
        PunchScript.Enabled = false
    elseif script.Parent.Parent:IsA("BackPack") then
        PunchScript.Enabled = true
    end
end)

Also you can’t change the PunchScript’s Enabled property in a local script, this would have to be in a server script.

So let’s make it server-sided:

local PunchScript = game.Players[script.Parent.Parent.Parent.Name].PlayerGui.Main.PunchScript or game.Players:GetPlayerFromCharacter(script.Parent.Parent).PlayerGui.Main.PunchScript

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
    if script.Parent.Parent:IsA("Model") then
        PunchScript.Enabled = false
    elseif script.Parent.Parent:IsA("BackPack") then
        PunchScript.Enabled = true
    end
end)

To get PunchScript, it looks for the script by the player or the character, depending if it’s equipped or not, to prevent errors.

Another thing to mention, in this image you can see a CreateBullet remote event. This can be easily exploited, and the person can just spam bullets with this remote event. I would add a cooldown that if surpassed will kick the player, using player:Kick("Message").

image

Hope this works!

I dont know if im doing something completely wrong (chance is I am) but this isnt working for me. Do I have to keep the PunchScript under the tool, or put it back where I had it before in starterGui

Is there an error message in your output?

Your PunchScript is in your tool now…

The script was looking for it in the GUI…

Put it back in there.

Out of curiosity, what is your reasoning behind using GetPropertyChangedSignal over Equipped/Unequipped? Doesn’t more logic make it more confusing and less explicit? Meriting the need for a heap of comments as opposed to just using a method/event pair that explicitly states what causes what in the code?

The equipping function is basically checking if your Tool is parented in a character, which what my custom function does.