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

How do i clone the same object multiple times?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Ad

Answer this question