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

Why does my script forget one part of each name?

Asked by 8 years ago

So I've made a script that goes through all of the parts and adds those parts to specific tables based on names. I'm doing this to build a table of parts and positions for the server to send to client for a minimap. To reduce on lag of the constant calling and waiting to get those parts. Yet, it has come to my attention that it seems to miss one part in each name category. I'm unaware of if it's in the table creation or the drawing that is causing this, and I've tried my best to find it. So I'm asking for help.

I do apologize ahead of time for the length of the scripts, but I figure the entire thing is needed.

Script

--======================--
--// Global Functions \\--
--======================--
local tableFunc = game.ReplicatedStorage.CrossConnections.Shared.TableFunction

local walls, floors, paths = {}, {}, {}

function getChildren(parent)
    for i, v in pairs(parent:GetChildren()) do
        if v:IsA("Part") then
            if string.lower(v.Name) == "wall" then
                table.insert(walls, #walls, {posX = v.Position.X, posZ = v.Position.Z, sizeX = v.Size.X, sizeZ = v.Size.Z, rotation = v.Rotation.Y})
            end
            if string.lower(v.Name) == "floor" then
                table.insert(floors, #floors, {posX = v.Position.X, posZ = v.Position.Z, sizeX = v.Size.X, sizeZ = v.Size.Z, rotation = v.Rotation.Y})
            end
            if string.lower(v.Name) == "path" then
                table.insert(paths, #paths, {posX = v.Position.X, posZ = v.Position.Z, sizeX = v.Size.X, sizeZ = v.Size.Z, rotation = v.Rotation.Y})
            end
        elseif v:IsA("Model") then
            getChildren(v)
        end
    end
end

function updateTables()
    getChildren(game.Workspace)
end

updateTables()

function tableFunc.OnServerInvoke(player, tableType)
    print(player.Name, tableType)
    if tableType == "walls" then
        for i = 1, #walls do
            for a, b in pairs(walls[i]) do
                tableFunc:InvokeClient(player, a, b, "wall")
            end
        end
    elseif tableType == "floors" then
        for i = 1, #floors do
            for a, b in pairs(floors[i]) do
                tableFunc:InvokeClient(player, a, b, "floor")
            end
        end
    elseif tableType == "paths" then
        for i = 1, #paths do
            for a, b in pairs(paths[i]) do
                tableFunc:InvokeClient(player, a, b, "path")
            end
        end
    end
end


--===================--
--// Map Functions \\--
--===================--
local getItems = game.ReplicatedStorage.CrossConnections.Map.GetItems

function getItems.OnServerInvoke(player, itemType)
    if itemType == "walls" then
        return walls
    elseif itemType == "floors" then
        return floors
    elseif itemType == "paths" then
        return paths
    elseif itemType == "self" then
        return game.Workspace:WaitForChild(player.Name).Torso.Position.X.. "::" ..game.Workspace:WaitForChild(player.Name).Torso.Position.Z
    end
end

LocalScript:

--==========================--
--// Predefined Variables \\--
--==========================--
local Players = game.Players
local Player = game.Players.LocalPlayer
local map = script.Parent.holder

local pX, pZ = 0,0

local dist = 65 --How far away items will be drawn

local unit0 = Vector3.new(0,0,1) --Look vector for north
local up = Vector3.new(0,1,0) --Look Vector for up
local degree = 57.2958 --Degree of 1 Radian

--=======================--
--// Predefined Clones \\--
--=======================--
local blip = script.Parent.Center:Clone()
blip.BackgroundColor3 = Color3.new(math.random(1, 255)/255, math.random(1, 255)/255, math.random(1, 255)/255)
blip.ZIndex = 2
blip.Visible = true

local name = script.Parent.You:Clone()
name.ZIndex = 2
name.Visible = true

--======================--
--// Remote Functions \\--
--======================--
local getItems = game.ReplicatedStorage.CrossConnections.Map.GetItems
local tableFunc = game.ReplicatedStorage.CrossConnections.Shared.TableFunction

local walls, floors, paths = {}, {}, {}

local wallCount = 0;
local floorCount = 0;
local pathCount = 0;

function tableFunc.OnClientInvoke(name, value, type)
    if name == "rotation" then
        if type == "wall" then
            wallCount = wallCount + 1
            table.insert(walls, wallCount, {})
        elseif type == "floor" then
            floorCount = floorCount + 1
            table.insert(floors, floorCount, {})
        elseif type == "path" then
            pathCount = pathCount + 1
            table.insert(paths, pathCount, {})
        end
    end

    if type == "wall" then
        walls[wallCount][name] = value
    elseif type == "path" then
        paths[pathCount][name] = value
    elseif type == "floor" then
        floors[floorCount][name] = value
    end
end

--======================--
--// Rotation Methods \\--
--======================--
function setRadarRotation(lv)
    local cp = lv*Vector3.new(1,0,1)
    local cr = up:Cross(unit0)

    local ang = math.acos(cp.unit:Dot(unit0))
    ang = (cp.unit:Dot(cr) < 0) and 2*math.pi - ang or ang

    script.Parent.holder.Rotation = ang*degree

    for i, blip in pairs(script.Parent.holder:GetChildren()) do
        blip.Rotation = 360-script.Parent.holder.Rotation
    end
end

function getRotationOfPart(rotY)
    if rotY == 90 or rotY == -90 then
        return 90
    else
        return 0
    end
end

--==================--
--// Draw Methods \\--
--==================--
function fixPos(blip, x, z)
    if x == 1 then
        blip.Position = blip.Position - UDim2.new(0,0,0,z/2)
    elseif z == 1 then
        blip.Position = blip.Position - UDim2.new(0,x/2,0,0)
    elseif z == 1 and x == 1 then
    else
        blip.Position = blip.Position - UDim2.new(0,x/2,0,z/2)
    end
end

function drawItem(itemName, posX, posZ, sizeX, sizeZ, rotY)
    if posX >= pX - dist and posX <= pX + dist and posZ >= pZ - dist and posZ <= pZ + dist then
        local newBlip = blip:Clone()
        newBlip.Position = blip.Position + UDim2.new(0, pX - posX, 0, pZ - posZ)
        newBlip.Parent = map
        newBlip.Rotation = getRotationOfPart(rotY)
        newBlip.Name = itemName
        newBlip.Size = UDim2.new(0, sizeX, 0, sizeZ)
        fixPos(newBlip, sizeX, sizeZ)

        if itemName == "wall" then
            newBlip.ZIndex = 2
        elseif itemName == "path" or itemName == "floor" then
            newBlip.ZIndex = 1
        end
    end
end

--============--
--// Tables \\--
--============--
wait(1) --Giving the server time to populate the tables if player is first in game
tableFunc:InvokeServer("walls")
tableFunc:InvokeServer("floors")
tableFunc:InvokeServer("paths")

--===================--
--// Render Method \\--
--===================--
function updateMap()
    map:ClearAllChildren()

    local youBlip = blip:Clone()
    youBlip.Visible = true
    youBlip.Parent = map

    local youName = name:Clone()
    youName.Parent = map
    youName.Visible = true

    setRadarRotation(game.Workspace.CurrentCamera.CoordinateFrame.lookVector)

    for i, wall in pairs(walls) do
        drawItem("wall", wall["posX"], wall["posZ"], wall["sizeX"], wall["sizeZ"], wall["rotation"])
    end
    for i, Floor in pairs(floors) do
        drawItem("floor", Floor["posX"], Floor["posZ"], Floor["sizeX"], Floor["sizeZ"], Floor["rotation"])
    end
    for i, path in pairs(paths) do
        drawItem("path", path["posX"], path["posZ"], path["sizeX"], path["sizeZ"], path["rotation"])
    end
end

while wait() do
    local selfPos = getItems:InvokeServer("self")
    pX, pZ = tonumber(string.sub(selfPos, 0, string.find(selfPos, "::")-1)), tonumber(string.sub(selfPos, string.find(selfPos, "::")+2, string.len(selfPos)))

    updateMap()
end

Answer this question