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

How do I add up values?

Heya guys, I wanted to make a tool that increases a value whenever activated, this is the script I tried. (I used Roblox's script template.)

local tool = script.Parent
 
local function onEquip()
end
 
local function onUnequip()
end
 
local function onActivate()
	game.Players.LocalPlayer.Character.Humanoid.BodyWidthScale.Value = 
end
 
local function onDeactivate()
end
 
tool.Equipped:Connect(onEquip)
tool.Unequipped:Connect(onUnequip)
tool.Activated:Connect(onActivate)
tool.Deactivated:Connect(onDeactivate)

For example, the value is 1. When the tool is activated, it increases to 2. And then when it is activated again, it's 3 and so on.

1 answer

local tool = script.Parent
local UserInputService = game:GetService("UserInputService")

local Value = 1 --Starting value

function onEquip()
	UserInputService.InputChanged:Connect(function(input) 
		if input.UserInputType == Enum.UserInputType.MouseButton1 then --This makes it so it detects when a mouse button is clicked.
            onActivate()
        end
    end)
end

function onUnequip()
end

function onActivate()
    Value = Value + 1 --Adding a number to the value when being eqquiped.
    game.Players.LocalPlayer.Character.Humanoid.BodyWidthScale.Value = Value -- Updating the "BodyWidthScale.Value" to the new, and updated value.

end

function onDeactivate()
end

tool.Equipped:Connect(onEquip)
tool.Unequipped:Connect(onUnequip)
tool.Activated:Connect(onActivate)
tool.Deactivated:Connect(onDeactivate)

Explanations are above

7 comments

0

When I tested the script, it increases the value whenever it's equipped. I wanted it to increase the value when the player CLICKS(Activates) the tool.

0

Great, then don't be a script kitty, and move the obvious addition line to where it needs to be

0

Yes ^, however it seems I cannot get around the :Activated() event, so I made a further revision in the script above which should make it so the value is increasing when the tool is equipped and is clicked.

0

I'm sorry for being one- I had no idea I was acting like that. Yes, I tried switching the addition line, but it doesn't work either-

0

Zach, try to use the updated code above.

0

I'm afraid it still doesn't work ^, I tried clicking a bunch of times.

0

Works when I do it.

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