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

How do I add a limit to part size??

Asked by
colvyr -5
3 years ago

I made a script for a hammer tool which's purpose is whenever I hit a specific part (called Wall1) it increases in specifically vertical/Y part size by using

Wall1.size = Wall1.size + Vector3.new(0,1,0)

But now I have 2 issues.

  1. The size keeps going and doesn't stop, I want the Wall1 size to stop at 0,5,0 in Vector3
  2. The size increases when I hit ANYTHING, not just the Wall1. I want the size to increase only when I hit Wall1 with the Hammer.

How do I solve both of these?

Hammer Tool Script

local Tool = script.Parent
local HammerHead = Tool.HammerHead
local Wall1 = game.Workspace.Wall1

local function onTouch(partTouched)
    local canBuild = partTouched.Parent:FindFirstChild("canBuild")
    if canBuild == partTouched.Parent:FindFirstChild("canBuild") then
        Wall1.Size = Wall1.Size + Vector3.new(0,1,0)
    end
end

HammerHead.Touched:Connect(onTouch)

1 answer

Log in to vote
0
Answered by 3 years ago

Can't guarantee this will work:

In a module script somewhere:

return {
    workspace.Wall1 = { MaxSize = Vector3.new(X, Y, Z), IncrementAmount = Vector3.new(0, 1, 0) },
    -- and so on
}

In the script:

local module = require(path.To.Module)
local debounce = false

hammer.Touched:Connect(function(hit)
    if debounce then return end; debounce = true

    if module[hit] then
        hit.Size += module[hit].IncrementAmount;
        hit.Size = Vector3.new(math.clamp(hit.Size.X, 0, module[hit].MaxSize.X), math.clamp(hit.Size.Y, 0, module[hit].MaxSize.Y), math.clamp(hit.Size.Z, 0 module[hit].MaxSize.Z))
    end

    wait(3)
    debounce = false
end)
Ad

Answer this question