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

How do I stop my tool's local scripts from conflicting?

Asked by 6 years ago

Hey, I have made 2 drinks tools, one makes you shoot up in the air and the other makes you run fast. When you drink the one that makes you shoot in the air, it works fine, but if I use the one that makes you run fast afterwards, it also shoots you up on the air. If I use the one that makes you run fast first, it works as intended and the one that makes you shoot up in the air works as intended, it somehow conflicts if I use the air one first.

They are both the same model, but are different colours.

Here's the code:

Speed LocalScript:

-- Tool Variables
local tool = script.Parent
local handle = tool:WaitForChild("Handle")


-- Local Player Variables --
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character

local human = plr.Character:WaitForChild("Humanoid")
local mouse = plr:GetMouse()


-- Animation Variables --
local drinkAnim= Instance.new("Animation")
drinkAnim.AnimationId= "rbxassetid://1294065907"

local animDrink = human:LoadAnimation(drinkAnim)


-- Drink Variables -- 
local speed = 32


-- Tool Equipped --
tool.Equipped:Connect(function()
    if  human.Health <= 0 then return end

    -- Drink And Play Animation --
    mouse.Button1Down:Connect(function()
        if human.Health <= 0 then return end
        human.WalkSpeed = 0     
        animDrink:Play()
        wait(2)
        human.WalkSpeed = speed
        wait(20)
        human.WalkSpeed = 16
    end)
end)

Air LocalScript


-- Tool Variables local tool = script.Parent local handle = tool:WaitForChild("Handle") -- Local Player Variables -- local plr = game.Players.LocalPlayer repeat wait() until plr.Character local character = plr.Character local human = character:WaitForChild("Humanoid") local mouse = plr:GetMouse() -- Animation Variables -- local drinkAnim= Instance.new("Animation") drinkAnim.AnimationId= "rbxassetid://1294065907" local animDrink= human:LoadAnimation(drinkAnim) -- Drink Variables -- -- NA function rocketJump() if character and character:findFirstChild("Humanoid") then local b = Instance.new("BodyPosition") b.position = Vector3.new(0, 600, 0) b.maxForce = Vector3.new(0, 500000000, 0) b.Parent = character.UpperTorso wait(3) b.Parent = nil end end -- Tool Equipped -- tool.Equipped:Connect(function() if human.Health <= 0 then return end -- Drink Potion And Play Animation -- mouse.Button1Down:Connect(function() if human.Health <= 0 then return end human.WalkSpeed = 0 animDrink:Play() wait(2) rocketJump() human.WalkSpeed = 16 end) end)
0
19 local animDrink= human:LoadAnimation(drinkAnim) Khornos 0 — 6y
0
What about it? RetroSpock 10 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

The problem is that when you are equipping either tool, you are creating an RBXScriptConnection, which listens whenever the player clicks and fires the attached function. However, the function doesn't check to see if the current tool is actually equipped, so the code will run any time the tool was equipped for the first time, even if the tool is unequipped! You can fix this by 'disconnecting' the connection for each respective tool when it is unequipped.

Modify both tools to include the following change:

...

local clickConnection

-- Tool Equipped --
tool.Equipped:Connect(function()
    if  human.Health <= 0 then return end
    -- Drink Potion And Play Animation --
    clickConnection = mouse.Button1Down:Connect(function()
        ...
    end)
end)

-- Tool Unequipped --
tool.Unequipped:Connect(function()
    -- Disconnect the click connection
    if clickConnection then
        clickConnection:Disconnect()
        clickConnection = nil
    end
end)
0
Thanks, I didn't think of unequipped function, though this didn't work :( I'll have to keep brute forcing a solution but you've definitely pointed me into the right direction. Any idea why this didn't work? Thanks again RetroSpock 10 — 6y
0
Ahhh Sorry, I missed the changes you did to equipped. It worked amazingly, thanks man! RetroSpock 10 — 6y
0
Glad 2 help ',:) WillieTehWierdo200 966 — 6y
Ad

Answer this question