Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i clone the same object multiple times?

Hello, im trying to make a spell in my game that shoots multiple shards out. Unfortunately i dont know how to make the object for the shard get spawned multiple times. The current code i have simply shoots out one if i left click. Here's the code.

--Force Shard
local shard = game.ServerStorage.Spells:WaitForChild("Shard")
local hurtSound = game.ReplicatedStorage:WaitForChild("Hurt")
game.ReplicatedStorage.Shard.OnServerEvent:Connect(function(player)
	local character = player.Character
	local newShard = shard:Clone()
	newShard.CFrame = character.RightHand.CFrame
	
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = Vector3.new(5000,5000,5000)
	bodyVelocity.Velocity = (character.HumanoidRootPart.CFrame.lookVector*100)
	bodyVelocity.Parent = newShard
	
	newShard.Parent = workspace
	newShard.CanCollide = true
	local touchConn
	local shardDMG = player.PlayerData.ShardDMG.Value
	local deBounceIce = false
	

	touchConn = newShard.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent.Name ~= player.Name then
				hurtSound:Play()
				if not deBounceIce then
					deBounceIce = true
					hit.Parent.Humanoid:TakeDamage(15 + shardDMG)
					deBounceIce = false
				end
				if touchConn ~= nil then touchConn:Disconnect() end
				newShard:Destroy()
			end
		end
	end)
	
	wait(2)
	
	if touchConn ~= nil then touchConn:Disconnect() end
	newShard:Destroy()
end)

1 answer

✓ Accepted answer

Do a for loop! I made the script for you cause why not:

for i = 1, [times_to_clone] do
local newShard = shard:Clone()
newShard.CFrame = character.RightHand.CFrame

    local bodyVelocity = Instance.new("BodyVelocity")
    bodyVelocity.MaxForce = Vector3.new(5000,5000,5000)
    bodyVelocity.Velocity = (character.HumanoidRootPart.CFrame.lookVector*100)
    bodyVelocity.Parent = newShard

    newShard.Parent = workspace
    newShard.CanCollide = true
    local touchConn
    local shardDMG = player.PlayerData.ShardDMG.Value
    local deBounceIce = false


    touchConn = newShard.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            if hit.Parent.Name ~= player.Name then
                hurtSound:Play()
                if not deBounceIce then
                    deBounceIce = true
                    hit.Parent.Humanoid:TakeDamage(15 + shardDMG)
                    deBounceIce = false
                end
                if touchConn ~= nil then touchConn:Disconnect() end
                newShard:Destroy()
            end
        end
    end)

    wait(2)

    if touchConn ~= nil then touchConn:Disconnect() end
    newShard:Destroy()
	end)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.