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

Trying to get the height of the left leg?

Asked by 2 years ago

I'm making a tower defense game, and i'm not sure how to get the height of the left leg of a rig. Any help? (Line 82)!

local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local modules = ReplicatedStorage:WaitForChild("Modules")
local health = require(modules:WaitForChild("Health"))

local money = Players.LocalPlayer:WaitForChild("Money")
local functions = ReplicatedStorage:WaitForChild("Functions")
local requestTowerFunction = functions:WaitForChild("RequestTower")
local spawnTowerFunction = functions:WaitForChild("SpawnTower")
local towers = ReplicatedStorage:WaitForChild("Towers")
local camera = workspace.CurrentCamera
local gui = script.Parent
local map = workspace:WaitForChild("Grassy")
local base = map:WaitForChild("Base")
local info = workspace:WaitForChild("Info")

local hoveredInstance = nil
local selectedTower = nil
local towerToSpawn = nil
local canPlace = false
local rotation = 0
local placedTowers = 0
local maxTowers = 15

local function SetupGui()
    health.Setup(base, gui.Info.Health)

    workspace.Mobs.ChildAdded:Connect(function(mob)
        health.Setup(mob)
    end)

    info.Message.Changed:Connect(function(change)
        gui.Info.Message.Text = change
        if change == "" then
            gui.Info.Message.Visible = false
        else
            gui.Info.Message.Visible = true
        end
    end)

    info.Wave.Changed:Connect(function(change)
        gui.Info.Stats.Wave.Text = "Wave " .. change
    end)

    money.Changed:Connect(function(change)
        gui.Info.Stats.Money.Text = change.."$"
    end)

    gui.Info.Stats.Money.Text = money.Value.."$"
end

SetupGui()

local function MouseRaycast(blacklist)
    local mousePosition = UserInputService:GetMouseLocation()
    local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
    local raycastParams = RaycastParams.new()

    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    raycastParams.FilterDescendantsInstances = blacklist

    local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)

    return raycastResult 
end

local function RemovePlaceholderTower()
    if towerToSpawn then
        towerToSpawn:Destroy()
        towerToSpawn = nil
        rotation = 0
    end
end

local function CreateRangeCircle(tower)

    local range = tower.Config.Range.Value
    local height = (tower.PrimaryPart.Size.Y / 2) + tower["Left Leg"].Height.Y
    local offset = CFrame.new(0, -height, 0)
    local p = Instance.new("Part")
    p.Name = "Range"
    p.Shape = Enum.PartType.Cylinder
    p.Material = Enum.Material.ForceField
    p.BrickColor = BrickColor.new("Electric blue")
    p.Size = Vector3.new(2,range * 2, range * 2)
    p.TopSurface = Enum.SurfaceType.Smooth
    p.BottomSurface = Enum.SurfaceType.Smooth
    p.CFrame = tower.PrimaryPart.CFrame * offset * CFrame.Angles(0, 0, math.rad(90))
    p.CanCollide = false
    p.Anchored = true
    p.Parent = workspace.Camera
end

local function AddPlaceholderTower(name)

    local towerExists = towers:FindFirstChild(name)
    if towerExists then
        RemovePlaceholderTower()
        towerToSpawn = towerExists:Clone()
        towerToSpawn.Parent = workspace.Towers

        for i, object in ipairs(towerToSpawn:GetDescendants()) do
            if object:IsA("BasePart") then
                PhysicsService:SetPartCollisionGroup(object, "Towers")
                object.Material = Enum.Material.ForceField 
            end
        end
    end
end

local function ColorPlaceholderTower(color)
    for i, object in ipairs(towerToSpawn:GetDescendants()) do
        if object:IsA("BasePart") then
            object.Color = color
        end
    end
end


gui.Towers.Title.Text = placedTowers.. "/" ..maxTowers
for i, tower in pairs(towers:GetChildren()) do
    if tower:IsA("Model") then
        local button = gui.Towers.Template:Clone()
        local config = tower:WaitForChild("Config")
        button.Name = tower.Name
        button.Image = config.Image.Texture
        button.Visible = true
        button.LayoutOrder = config.Price.Value
        button.Price.Text = config.Price.Value.."$"

        button.Parent = gui.Towers

        button.Activated:Connect(function()
            local allowedToSpawn = requestTowerFunction:InvokeServer(tower.Name)
            if allowedToSpawn then
                AddPlaceholderTower(tower.Name)
            end
        end)
    end
end

local function toggleTowerInfo()
    if selectedTower then
        CreateRangeCircle(selectedTower)
        gui.Selection.Visible = true
        local config = selectedTower.Config
        gui.Selection.Stats.Damage.Value.Text = config.Damage.Value
        gui.Selection.Stats.Range.Value.Text = config.Range.Value
        gui.Selection.Stats.Cooldown.Value.Text = config.Cooldown.Value
        gui.Selection.Title.TowerName.Text = selectedTower.Name
        gui.Selection.Title.TowerImage.Image = config.Image.Texture

        local upgradeTower = config:FindFirstChild("Upgrade")
        if upgradeTower then
            gui.Selection.Action.Upgrade.Visible = true
            gui.Selection.Action.Upgrade.Text = "UPGRADE: " ..upgradeTower.Value.Config.Price.Value.."$"
        else
            gui.Selection.Action.Upgrade.Visible = false
        end
    else
        gui.Selection.Visible = false
    end
end

gui.Selection.Action.Upgrade.Activated:Connect(function()
    if selectedTower then
        local upgradeTower = selectedTower.Config.Upgrade.Value
        local allowedToUpgrade = requestTowerFunction:InvokeServer(upgradeTower.Name)

        if allowedToUpgrade then
            selectedTower = spawnTowerFunction:InvokeServer(upgradeTower.Name, selectedTower.PrimaryPart.CFrame, selectedTower)
            toggleTowerInfo()
        end
    end
end)

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then
        return
    end

    if towerToSpawn then
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            if canPlace then
                local placedTower = spawnTowerFunction:InvokeServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
                if placedTower then
                    placedTowers += 1
                    gui.Towers.Title.Text = placedTowers.. "/" ..maxTowers
                    RemovePlaceholderTower()
                    toggleTowerInfo()
                end
            end
        elseif input.KeyCode == Enum.KeyCode.R then
            rotation += 90
        end
    elseif hoveredInstance and input.UserInputType == Enum.UserInputType.MouseButton1 then
        local model = hoveredInstance:FindFirstAncestorOfClass("Model")

        if model and model.Parent == workspace.Towers then
            selectedTower = model
        else
            selectedTower = nil
        end

        toggleTowerInfo()
    end

end)

RunService.RenderStepped:Connect(function()
    local result = MouseRaycast({towerToSpawn})
    if result and result.Instance then
        if towerToSpawn then
            hoveredInstance = nil

            if result.Instance.Parent.Name == "TowerArea" then
                canPlace = true
                ColorPlaceholderTower(Color3.new(0,1,0.6))
            else
                canPlace = false
                ColorPlaceholderTower(Color3.new(1,0,0))
            end
            local x = result.Position.X
            local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 0.65)
            local z = result.Position.Z

            local cframe = CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation), 0)
            towerToSpawn:SetPrimaryPartCFrame(cframe)
        else
            hoveredInstance = result.Instance
        end
    else
        hoveredInstance = nil
    end

end)

Answer this question