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

Raycast camera just returns blue. Please help?

Asked by 7 years ago

im trying to make a camera in roblox with raytracing. it isnt working all that well. when i use it, the image comes out just as blue. can someone help? heres the server script:

--Made by AppleBLOXIAN

local folder = script.Parent
local tool = folder.Parent
local req = folder:WaitForChild("Request")
local capev = game.ReplicatedStorage:WaitForChild("OnImageCapture")
local settings = tool:WaitForChild("Settings")
local capheight = settings:WaitForChild("CaptureHeight")
local capwidth = settings:WaitForChild("CaptureWidth")

function getHex(num)
    local chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    local hex = ""
    local x = num / 16
    local y = num % 16

    hex = chars[math.floor(x) + 1] .. chars[math.floor(y) + 1]

    return hex
end

function color3ToHex(color3)
    if not color3["r"] then return "000000" end

    local r, g, b = color3.r * 255, color3.g * 255, color3.b * 255
    return "" .. getHex(r) .. getHex(g) .. getHex(b)
end

function capture()
    local image = {
        width = capwidth.Value,
        height = capheight.Value,
        data = {}
    }
    --init
    print("Initializing...")
    local rays; do
        rays = {}
        local halfwidth = image.width / 2
        local halfheight = image.height / 2
        local startpos = tool:WaitForChild("Handle").Position - Vector3.new(halfwidth*0.05,halfheight*0.05,0)
        print("Raycasting...")
        local cx = startpos.X
        local cy = startpos.Y
        local cpos = startpos
        for i = 1,image.height do
            for i = 1,image.width do
                local ray = Ray.new(cpos,tool:WaitForChild("Handle").CFrame.lookVector)
                local hit,pos,normal = workspace:FindPartOnRay(ray)
                if hit then
                    local color = color3ToHex(hit.Color)
                    image.data[#image.data+1] = color
                else
                    local color = color3ToHex(Color3.fromRGB(0,255,255))
                    image.data[#image.data+1] = color
                end
                cx = cx + 0.05
                cpos = Vector3.new(cx,cy,0)
            end
            cx = startpos.X
            cy = cy + 0.05
            cpos = Vector3.new(cx,cy,0)
        end
    end
    --handle image
    print("Compiling...")
    image = game:GetService("HttpService"):JSONEncode(image)
    print("Done!")
    capev:Fire(image)
end

function onServerEvent(plr,req)
    if req == "Capture" then
        capture()
    end
end

req.OnServerEvent:connect(onServerEvent)

and heres the local script:

--Made by AppleBLOXIAN

local folder = script.Parent
local tool = folder.Parent
local guis = tool:WaitForChild("GUIs")
local gui = guis:WaitForChild("ScreenGui")
local button = gui:WaitForChild("TextButton")
local req = folder:WaitForChild("Request")

function onEquip()
    gui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
end

function onUnEquip()
    gui.Parent = guis
end

function onClick()
    req:FireServer("Capture")
    spawn(function()
        button.Text = "Captured"
        wait(0.5)
        button.Text = "Capture"
    end)
end

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnEquip)
button.MouseButton1Click:connect(onClick)

and heres another server script displaying the image on a surfacegui:

local sgui = script.Parent
local imgfolder = sgui:WaitForChild("Image")
local capev = game.ReplicatedStorage:WaitForChild("OnImageCapture")

function hexToColor3(hex)
    local r,g,b = hex:sub(1,2),hex:sub(3,4),hex:sub(5,6)
    r,g,b = tonumber(r,16),tonumber(g,16),tonumber(b,16)
    return Color3.fromRGB(r,g,b)
end

function stringSplit(s)
    local tab = {}
    for w in s:gmatch("%S+") do
        tab[#tab+1] = w
    end
    return tab
end

function renderImage(x,y,hex)
    local hextab = stringSplit(hex)
    local cx = 1
    local cy = 1
    local frm; do
        frm = Instance.new("Frame")
        frm.Size = UDim2.new(0,x,0,y)
        frm.BackgroundTransparency = 1
    end
    spawn(function()
        for i,v in pairs(hextab) do
            local pixel; do
                pixel = Instance.new("Frame",frm)
                pixel.Size = UDim2.new(0,1,0,1)
                pixel.Position = UDim2.new(0,cx-1,0,cy-1)
                pixel.BackgroundColor3 = hexToColor3(v)
                pixel.BorderSizePixel = 0
            end
            if cx == x then
                cx = 1
                cy = cy+1
                coroutine.yield()
            else
                cx = cx+1
                if cx == x/4 then
                    coroutine.yield()
                end
            end
        end 
    end)
    return frm
end

function onEvent(json)
    imgfolder:ClearAllChildren()
    local imgdata = game:GetService("HttpService"):JSONDecode(json)
    local x = imgdata.width
    local y = imgdata.height
    local hex = table.concat(imgdata.data," ")
    local img = renderImage(x,y,hex)
    img.Parent = imgfolder
end

capev.Event:connect(onEvent)

Answer this question