luayoutube-apirobloxluau

Unable to clone and move the cloned model


I was trying to make an script that everytime someone subscrive to my Youtube channel, the parent (an noob model) gets cloned and teleported 8 studs UP, but that just dont work or just spawns the clone inside the original model.

Can someone help me please?

Also, please PLEASE sorry if the code is hard to understand, thats because all the coments are in another language (Brazilian Portuguese).

Here is my current LUA script:

--!strict
print("[YTLiveSubs]: Script inserido...")
local http = game:GetService('HttpService')

local channel_id = 'UCA4yZCVHlHZVknfesTU3XOg'
local url = 'https://api.socialcounts.org/youtube-live-subscriber-count/'..channel_id

local decoded
local subs
local diffsubs
local data
local success
local finaldiffsubs -- aquele negócio que mostra a diferença de incritos.

-- Ping
if script:GetAttribute("Debug") == true then -- Checa se o modo "Debug" foi ativado
    print("[YTLiveSubs]: Iniciando teste de ping!...")
    local startTime = os.clock()

    local success, response = pcall(function()
        return http:GetAsync("https://www.google.com")
    end)

    if success then
        local deltaTime = os.clock() - startTime
        print("Pong!: " .. deltaTime .. " segundos")
    else
        print("Erro: " .. response)
    end
end
-- Fim do Ping

function ChecarInscritos()
success,data = pcall(function()
    return http:GetAsync(url)
end)
    --if data then
    decoded = http:JSONDecode(data)
    subs = decoded["est_sub"]
    print(subs)
    --end
end

print("[YTLiveSubs]: Calculando First-run...")
ChecarInscritos()
while true do
    diffsubs = subs
    wait(3) -- em resumo, eu coloquei isso para não sobrecarregar a API
    -- Eu podia tranquilamente não ter feito isso, ou mudade o cooldown para 0.1 ou 0.5,
    -- Mas a API épública e eu não quero estragar o uso das outras pessoas. - PatoFlamejanteTV
    ChecarInscritos()
    print("[YTLiveSubs]: Inscritos atuais: " .. subs)
    --print("Pong!") -- Avisa que um novo "tick"
    print("[YTLiveSubs]: Novos inscritos: ".. subs - diffsubs) --matemáticas :nerd:
    finaldiffsubs = subs - diffsubs
    for i = 1, finaldiffsubs do
        local clone = game.Workspace.Noob:Clone()
        
        clone.Parent = game.Workspace
        local ogposition = script.Parent
        clone:PivotTo((0), (ogposition.PrimaryPart.CFrame - (Vector3.yAxis * 4)), (0))
        
        --clone:MoveTo(Vector3.new(0, 10, 0)) -- move 10 studs up in the y-axis from the origin
        clone.Name = "Noob"..i -- os "..i"s/"..i"ses (sla o plural) é pra fazer um
        clone.Humanoid.DisplayName = "Noob"..i -- efeito de "Noob1", "Noob2", etc.
        --clone.Humanoid.WalkSpeed = 16
        --clone.Humanoid.JumpPower = 50
        --clone.Humanoid.MaxHealth = 100
        --clone.Humanoid.Health = 100
        --clone.HumanoidRootPart.CFrame = game.Workspace.Noob.HumanoidRootPart -- nem eu sei o que é isso
        end
    print("[YTLiveSubs]: Spawned ".. finaldiffsubs .." Noobs!")
end




Solution

  • I recommend reading StackOverflow's article on how to produce a Minimal and Reproducible Sample. Regardless, Roblox's documentation on PivotTo shows that it takes in a single CFrame parameter. I am guessing that you want to create a stack of noobs that matches the number of subscribers that you have on YouTube.

    It seems as if you want to create a stack of noobs. In this case, you can determine the height of the current noob by doing a bit of math.

    for i = 1, finaldiffsubs do
        local clone = game.Workspace.Noob:Clone()
    
        clone.Parent = game.Workspace
        local ogposition = script.Parent
        clone:PivotTo(
            ogposition.PrimaryPart.CFrame * CFrame.new(0, 4 * (i + diffsubs), 0)
        )
        -- You can continue the code you had in the for loop here...
    end
    

    For a bit of explanation on what you were doing wrong, you were trying to construct a CFrame through passing three parameters into the PivotTo function, when you would need to pass these 3 numbers into the CFrame constructor. This would construct a CFrame object, which is exactly what PivotTo wants to take in as input. I went a step further and made the noob model have the same rotation as the ogposition part, however you can undo this by creating a CFrame by passing it a Vector3 (which is how Roblox represents 3D vectors for things such as position). and do something like:

    clone:PivotTo(
        CFrame.new(
            ogposition.PrimaryPart.Position + Vector3.new(0, 4 * (i + diffsubs), 0)
        )
    )