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

I need help fix the object Clone()?

Asked by 3 years ago
Edited 3 years ago

Hello, I have a problem with the code, namely when I try to Clone every 60 seconds an object from ServerStorage and copy its Parent to the workshop. The problem arises when you copy an object, every 60 seconds its copy is created infinity I wanted to set the IF to break it somehow but I don't know how to go about it, I tried all the ways with FindFirstChild break etc

Here Code pss i am newbie with coding if u are to know Don't laught if here get a mistakes:

local ServerStorage = game:GetService("ServerStorage") local parts = ServerStorage.TreasureChest local timer = 5 while wait(timer) do for _,chests in pairs(game.Workspace.Folder:GetChildren())do

    if not chests:FindFirstChild("TreasureChest") then
    print("its model doesn't exists!")
    -- Clone from ServerStorage  to Workspace folder
    local elo = parts:Clone()
    elo.Parent = game.Workspace.Folder
    local HealthChest = Instance.new("IntValue",parts)
    HealthChest.Name = "HealthChest"
        HealthChest.Value = tonumber(math.random(1250,2500))
    break
    elseif chests:IsA("Model") and chests.Name == "TreasureChest" then
    print("model exists!")
    chests:SetPrimaryPartCFrame(CFrame.new(-4,-10,math.random(0,150))* CFrame.Angles(0,math.random(0,360),0))       
    break
end
end

end

Here are image the problem are checked red color: https://ibb.co/S5rrXJK

1 answer

Log in to vote
0
Answered by 3 years ago

On line 5, when you say,

for _,chests in pairs(game.Workspace.Folder:GetChildren()) do

which basically identify "chests" as each child in game.Workspace.Folder. So basically what you are saying on 6 is, if you cannot find a child IN a child in game.Workspace.Folder called "TreasureChest", then create one. The problem is that I think you're trying to find something called "TreasureChest" in the folder itself, not one of the children in the folder. Try changing line 6 to

if game.Workspace.Folder:FindFirstChild("TreasureChest") then

and i think that will work

hope this helps

Ad

Answer this question