Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I fix this issue with the tool since I can't think of anything else?

Asked by 5 years ago

There's a problem with my local script inside this tool.If I use it everything works fine but once I change to another tool and back to this one, if I click it, everything happens twice.The more I change to another tool, the more times this happens.I've tried everything I could think of to stop this from happening but no result.If I'd use the tool when FE isn't enabled(Tool which would have the server script inside too of course) everything works smoothly.So I reckon I'm missing something since it doesn't work in FE. Here's the local script.No need for the server script since the problem is here. Thanks for your time.

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Event1")
local bin = script.Parent

local modules = require(game.Workspace.ModuleScript)
local player = game.Players.LocalPlayer

spew = false
mouse = player:GetMouse()
IsEquipped = false

function onSelected(mouse)
    IsEquipped = true
    print("select")
    mouse.Icon = "rbxasset://textures\\GunCursor.png"
    bin.Activated:connect(function() onButton1Down(mouse) end)
    bin.Deactivated:connect(function() onButton1Up(mouse) end)
end


function onButton1Down(mouse)
    spew = true
    while spew do
        if not IsEquipped then return end
        modules.Take(player, 1)
        wait(.08)
        mousehitp = mouse.Hit.p
        RemoteEvent:FireServer(mousehitp)
    end
end

function onButton1Up(mouse)
    spew = false
end

bin.Unequipped:Connect(function()
    IsEquipped = false
end)

bin.Equipped:connect(onSelected)

1 answer

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

The problem is that you are connecting the Activated and Deactivated functions in your onSelected, so every time that function is called, you have another set of Activated and Deactivated listeners, in addition to all the ones you previously connected.

Instead, you should connect those functions separately and have them only fire when a variable is true. That variable will become true when the tool is selected, and false when it is deselected. You actually already have this variable, IsEquipped. Here is an example of using it:

local isEquipped = false
local spew = false

bin.Equipped:Connect(function()
    isEquipped = true
    mouse.Icon = "rbxasset://textures\\GunCursor.png"
end)

bin.Unequipped:Connect(function()
    isEquipped = false
end)

bin.Activated:Connect(function()
    if not isEquipped then return end
    spew = true
    while spew do
        if not isEquipped then return end
        modules.Take(player, 1)
        wait(.08)
        mousehitp = mouse.Hit.p
        RemoteEvent:FireServer(mousehitp)
    end
end)

bin.Deactivated:Connect(function()
    if not isEquipped then return end
    spew = false
end)

In the above scripts, you could actually remove the first if not isEquipped then return end, but there's a possibility that spew would change from true and false - which causes no harm because it will not actually shoot the projectile since the while loop also contains that statement.

Hope this helps! :)

0
Thank you so much! Been struggling for the last 2 hours, you're a life saver! brokenrares 48 — 5y
Ad

Answer this question