The tool works, everything from clicking buttons and pressing down keys, as expected. But, if a player were to spam equip and unequip the said tool, clicking buttons would double the expected value. Say if I equip and unequip and then equip this tool back, and I click on Plus button, the value wouldn't go up by 0.1, but rather 0.2 or straight to the maximum. Any ideas why?
I've tried putting the mouse1buttonclick on the bottom of the script and only run the function if the tool is equipped, but it decided not to run at all.
For example, if I put a ToolSelect = false, and ToolSelect becomes true when the tool is equipped, and false when tool is unequipped. And then I put the condition to run the function when buttons are clicked is ToolSelect == true, but... nothing. Not working at all.
My main question is, how to disconnect the mousebutton1click when the tool is unequipped or make it not doubling the values?
local InfoGui = game.Players.LocalPlayer.PlayerGui.InfoGui.Frame local passId = 361436914 local player = game.Players.LocalPlayer local PlayerVIP = 0 local mode = nil local Targ = nil if (player.UserId >= 1) then isVIP = game:GetService("MarketplaceService"):PlayerOwnsAsset(player, passId) if (isVIP) then PlayerVIP = 1 end end repeat wait() until player.Character local value = 0 function onMinusClicked() if( mode ~= nil) then if mode == "Transparency" then if(value == 0.5) then InfoGui.Frame.MidLast.Text = "Press Q or E to change the transparency value" end value = math.max(value - 0.1, 0) if(Targ ~= nil) then Targ.Transparency = value end InfoGui.Frame.First.Text = "Transparency: "..value end end end function onPlusClicked() if(mode ~= nil) then if mode == "Transparency" then if (PlayerVIP == 1) then value = math.min(value + 0.1, 1) else value = math.min(value + 0.1, 0.5) if (value == 0.5) then InfoGui.Frame.MidLast.Text = "Only VIPs are allowed to set more than half of the transparency value!" end end InfoGui.Frame.First.Text = "Transparency: "..value if(Targ ~= nil) then Targ.Transparency = value end end end end function onKeyDown(key) if (key~=nil) then if (key=="e") then if (PlayerVIP == 1) then value = math.min(value + 0.1, 1) else value = math.min(value + 0.1, 0.5) if (value == 0.5) then InfoGui.Frame.MidLast.Text = "Only VIPs are allowed to set more than half of the transparency value!" end end end if (key=="q") then if(value == 0.5) then InfoGui.Frame.MidLast.Text = "Press Q or E to change the transparency value" end value = math.max(value - 0.1, 0) end InfoGui.Frame.First.Text = "Transparency: "..value if(Targ ~= nil) then Targ.Transparency = value end end end function onMoved(target) if target then if not target.Locked then if(target.Parent.Name == tostring(player.UserId)) then selection.Adornee = target end end end end function onClicked(target) if target then if not target.Locked then if(target.Parent.Name == tostring(player.UserId)) then Targ = target selection.Adornee = target target.Transparency = value else selection.Adornee = nil end else selection.Adornee = nil end else selection.Adornee = nil end end function onSelected(mouse) InfoGui.Visible = true InfoGui.Frame.Visible = true InfoGui.Frame.First.Visible = true selection = Instance.new("SelectionBox") selection.Parent = player.PlayerGui mouse.Button1Down:connect(function() onClicked(mouse.Target) end) mouse.Move:connect(function() onMoved(mouse.Target) end) mouse.KeyDown:connect(onKeyDown) InfoGui.Frame.MidLast.Visible = true InfoGui.Frame.MidLast.Text = "Press Q or E to change the transparency value" InfoGui.Frame.First.Text = "Transparency: "..value InfoGui.Frame.First.Minus.MouseButton1Click:connect(function() mode = "Transparency" onMinusClicked() end) InfoGui.Frame.First.Plus.MouseButton1Click:connect(function() mode = "Transparency" onPlusClicked() end) end function onDeselected() Targ = nil value = 0 InfoGui.Visible = false InfoGui.Frame.Visible = false InfoGui.Frame.MidLast.Visible = false InfoGui.Frame.First.Visible = false selection:remove() mode = nil end script.Parent.Equipped:connect(onSelected) script.Parent.Unequipped:connect(onDeselected)
The problem is that you're making multiple connections on the same event every time the player equips the tool (look at line 118
and line 119
).
You should connect these events somewhere else in your script, like near the bottom, below line 134
. In addition, you should set the Active
property of InfoGui.Frame.First.Minus
and InfoGui.Frame.First.Plus
to true
or false
every time the player equips and unequips the tool.
Another way of going about fixing this is to store these events in variables so that you can :disconnect()
them whenever the player unequips the tool.