Code issues - random parts

Hi, I need to make these parts randomly generated

for example make them stay close but randomly spawn just by changing their positions, how i do that? :frowning:

image

Correct me if Iā€™m wrong on what youā€™re asking.

Clone a part and if you need it to be close, pick a random number and add it to a partā€™s position.
Like this:

local orignialPart = --this is the part where all the parts will group together with
local clonedPart = Instance.new ("Part")
clonedPart.Parent = game.Workspace

clonedPart.Position = originalPart.Position + Vector3.new(math.random(0,10), 0, math.random(0,10)

If you need it to repeat - you can make it as many times as you want with this:

local orignalPart = -- again, this is what the parts will revolve around

for i = 1,2 do --the two is an example I used just change the number (2) to your liking
   local part = originalPart:Clone()
   part.Position = originalPart.Position + Vector3.new(math.random(0,10), 0, math.random(0,10)
end

Did that solve your problem?

3 Likes

I tried the script and the part was not cloned :frowning: I donā€™t know what Iā€™m doing wrong ā€¦

image

image

code:

local Player = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
	if PlayerCharacter then
		
		
		local orignialPart = game.Workspace.Coins
		local clonedPart = Instance.new ("Part")
		clonedPart.Parent = game.Workspace
		clonedPart.Position = orignialPart.Position + Vector3.new(math.random(0,10), 0, math.random(0,10))
		
		local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
		Leaderstats.Cash.Value =  Leaderstats.Cash.Value + 10 
		script.Parent:Destroy()
	end
end)

I need that part to be cloned near it and randomly generated

While I open Roblox studio, try replacing all of the ā€œPositionā€ with ā€œCFrameā€.

Like this.

local Player = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
	if PlayerCharacter then
		
		
		local originalPart = game.Workspace.Coins
		local clonedPart = originalPart:Clone()
		clonedPart.Parent = game.Workspace
		clonedPart.CFrame = originalPart.CFrame + Vector3.new(math.random(0,10), 0, math.random(0,10))
		
		local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
		Leaderstats.Cash.Value =  Leaderstats.Cash.Value + 10 
		script.Parent:Destroy()
	end
end)

I changed a few other things in case that was the problem.

image

didnā€™t cloned anywhere :frowning:

oh at the moment of touching her she cloned, I need her to clone herself before having touched her, how would that be?

Any errors? Also maybe the part is spawning below the baseplate so it is just being destroyed. Try changing this line:

clonedPart.Position = orignialPart.Position + Vector3.new(math.random(0,10), 0, math.random(0,10))
		

Change it to this:

clonedPart.Position = orignialPart.Position + Vector3.new(math.random(0,10), 10, math.random(0,10))

I had some typos, I fixed it. Did that fix it?

1 Like

Also, it doesnā€™t affect the Y-axis, I did that on purpose, so that shouldnā€™t be the problem.

I just saw it work and create the clone, but the clone does not give money like the other part

Oh if you want it to also give money then do this:

local Player = game:GetService("Players")

local db = false
script.Parent.Touched:Connect(function(hit)
        if db == false then
          db = true
	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
	if PlayerCharacter then
		local orignialPart = game.Workspace.Coins
		local clonedPart = script.Parent:Clone()
                clonedPart.Position = orignialPart.Position + Vector3.new(math.random(0,10), 5, math.random(0,10))
		clonedPart.Parent = game.Workspace
		local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
		Leaderstats.Cash.Value =  Leaderstats.Cash.Value + 10 
		script.Parent:Destroy()
	end
        wait(0.5)
        db = false
       end
end)
2 Likes

I found the issue. You seem to have added a:

script.Parent:Destroy()

at the end, causing it the original part to die, so there would be only one clone.

local Player = game:GetService("Players")

local db = false
script.Parent.Touched:Connect(function(hit)
    if db == false then
      db = true
	  local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
	  if PlayerCharacter then
		  local orignialPart = game.Workspace.Coins
		  local clonedPart = script.Parent:Clone()
          clonedPart.Position = orignialPart.Position + Vector3.new(math.random(-10,10), 0, math.random(-10,10))
		  clonedPart.Parent = game.Workspace
		  local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
		  Leaderstats.Cash.Value =  Leaderstats.Cash.Value + 10 
	  end
      wait(0.5)
      db = false
   end
end)

Unless you wanted it to be like that, then this should probably solve your problem.

2 Likes

Thank you guys so much! Really, thank you for all the support and the problem is solved :smiley:

2 Likes

Thanks for giving me the solution, but I think that @NoodleCrystal10 deserves it. He wrote most of the code, I just changed a couple lines.

1 Like

Itā€™s true, I just give him @NoodleCrystal10 thank you guys very much!

1 Like