[ SOLVED ] Scripting Issue With Dirt Pile System

Setup:
Trash Piles FOLDER - inside ServerStorage
Dirt 1-4 - inside Trash Piles FOLDER
image

Script: (Placed inside ServerScriptStorage)

local Dirt1 = game.ServerStorage["Trash Piles"].Dirt1
local Dirt2 = game.ServerStorage["Trash Piles"].Dirt2
local Dirt3 = game.ServerStorage["Trash Piles"].Dirt3
local Dirt4 = game.ServerStorage["Trash Piles"].Dirt4

while true do
    local randomDirt = math.random(1, 4)
    local Clone = nil
    if randomDirt == 1 then
        Clone = Dirt1:Clone()
    elseif randomDirt == 2 then
        Clone = Dirt2:Clone()
    elseif randomDirt == 3 then
        Clone = Dirt3:Clone()
    else
        Clone = Dirt4:Clone()
    end
    if Clone:FindFirstChild("Parent") == nil then
        while true do
		    local waitTime = math.random(30,50)
		    wait(waitTime)
		    Clone.Parent = game.Workspace
		    if Clone.Parent == game.Workspace then
			    Clone:Destroy()
			    break
		    end
	    end
    end
end

image

Problem, there is output if I print hello world, the problem is that no piles spawn whatsoever, it’s left empty and it just runs the script without any piles spawning inside Workspace. The goal is to make the piles spawn in Workspace preferably still being inside the “Trash Piles” folder, why does this happen?

2 Likes

line 22 is setting the clone’s parent to workspace and line 23 is checking if the clone’s parent is workspace, which will always be true, so Clone will always be destroyed instantly

4 Likes

Another thing on line 18, thing.FindFirstChild("Parent") is not the same as thing.Parent. The . operator does not “get the child with the name”, it does something different depending on what the text after is. If it’s one of the properties of the object, then it gets the value of that even if it’s nil. Otherwise it tries to find any child with that name, and if that fails too then it errors. So thing.Parent never looks for a child called “Parent” (which would be a terrible name BTW) because all Instances have a property called “Parent”. So you can’t do thing.FindFirstChild("Parent") in place of thing.Parent, it doesn’t do the same thing not even close. Are you trying to get the parent of Clone or are you really looking for a child called “Parent”?

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.