Model cloning issue

Hi there. I’m trying to make a part that can be stepped on to be able to get models into the workspace by cloning them from replicated storage into the workspace once a player steps on said part. Problem is that I run into an issue regarding my code for it.
The part to be stepped on looks something like this (as reference of location):


Once a player steps on said part, it will generate a new part or “area”.
This is the code that is being used in the part to clone the models from replicated storage:

script.Parent.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
	local radnumb = math.random(1,2)
	if radnumb == 1 then
		radnumb = "Grasslands"
	local repstore = game:GetService("ReplicatedStorage")
	local part = repstore:GetChildren()
		if part.Name == radnumb then
			part:Clone()
		local primaryp = script.Parent.Parent.PrimaryPart
		part.Parent = game.Workspace
		part:SetPrimaryPartCFrame(primaryp.CFrame)
		part:MoveTo(primaryp.Position + Vector3.new(0, 0, 30))
		print("Script will now be destroyed.")
		script:Destroy()
	else
			warn(radnumb.." did not load. Replication Failure!")
			end
	elseif radnumb == 2 then
		radnumb = "Snow Biome"
	local repstore = game:GetService("ReplicatedStorage")
	local part = repstore:GetChildren()
		if part.Name == radnumb then
			part:Clone()
		local primaryp = script.Parent.Parent.PrimaryPart
		part.Parent = game.Workspace
		part:SetPrimaryPartCFrame(primaryp.CFrame)
		part:MoveTo(primaryp.Position + Vector3.new(0, 0, 30))
		print("Script will now be destroyed.")
		script:Destroy()
	else
		warn(radnumb.." did not load. Replication Failure!")
	end
	
	end
end
end)

image
For whatever reason, I’m not shown any errors whatsoever regarding the code but it seems to not work for some distinct lines, supposedly around in these areas:
image
Don’t really know how to approach how to fix this so any help would be appreciated! (I’ve listed this issue on the devforum before but I unintentionally didn’t clarify that this is in fact NOT terrain generation. This is actual model cloning)

1 Like

I dont believe this is how you change a clones property.

local part = repstore:GetChildren()
		if part.Name == radnumb then
			part:Clone()
		local primaryp = script.Parent.Parent.PrimaryPart
		part.Parent = game.Workspace
		part:SetPrimaryPartCFrame(primaryp.CFrame)

I believe you need to set the clone to a variable than change the clone using the variable

local part = repstore:GetChildren()
		if part.Name == radnumb then
			local clonedpart = part:Clone()
		local primaryp = script.Parent.Parent.PrimaryPart
		clonedpart.Parent = game.Workspace
		clonedpart:SetPrimaryPartCFrame(primaryp.CFrame)
1 Like

change it to

local clonedPart = part:Clone()

On both side of the scripts

Edit: just saw @Wizard101fire90 said this aswell

1 Like

I tried that actually and it didn’t work. It jumped to the “warn(radnumb…” did not load. Replication Failure!“)” lines of code that I put in case of things like this happening but didn’t give me any actual output errors like usual.

1 Like

You need to loop through the part not just directly compare thepart.Name with your radnumd

local repstore = game:GetService("ReplicatedStorage")
local part = repstore:GetChildren()
for _, child in pairs(part) do
	if child.Name == radnumb then
		local clone = child:Clone()
		local primaryp = script.Parent.Parent.PrimaryPart
		child.Parent = game.Workspace
		-- do the rest here
	else
		warn(radnumb.." did not load. Replication Failure!")
	end
end

Try that for each of the radnumb conditional blocks

1 Like

Just tried your suggestion. Works just fine. Thank you!!

1 Like

You’re welcome. I just want to add that Touched event is tends to trigger multiple times. You probably want to add debounce to it.

1 Like