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

[Edited x2]How to make clones of the same part unique?

Asked by 5 years ago
Edited 5 years ago

I have made a script that creates multiple spikes from the ground, and I want to make each one of them individual in the sense that if a spike is touched, that spike gets destroyed. However I cant seem to figure out to do that, when I touch a spike, another spike is destroyed.

game.ReplicatedStorage.EarthStomp.OnServerEvent:Connect(function(player)
local char = game.Players[player.Name].Character
    local hum = char.Humanoid
    local root = char.HumanoidRootPart
    local ar = Instance.new("Part")
    local ten = true
    ar.BrickColor = BrickColor.new("Bright green")
    ar.Anchored = true
    ar.CanCollide = false
    ar.Material = Enum.Material.Neon
    ar.Transparency = 0
    ar.Shape = Enum.PartType.Cylinder
    ar.Size = Vector3.new(0.01,1,1)
    ar.CFrame = player.Character["Left Leg"].CFrame + Vector3.new(0.01,0,0)
    ar.Orientation = Vector3.new(0, 0, 90)
    ar.Parent = workspace
    game.Debris:AddItem(ar,2)
    local tween = game:GetService("TweenService")
    local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
    local tweenProperty = {Size = Vector3.new(0.01,150,150);Transparency = 1}
    local tweenRun = tween:Create(ar,tweenInfo,tweenProperty)
    tweenRun:Play()

    wait(0.5)



function earthspike()

    earthspike = game:GetService("ServerStorage").Spike:Clone()
    earthspike.Anchored = true
    earthspike.CanCollide = true
    earthspike.Parent = workspace
    earthspike.CFrame = ar.CFrame * CFrame.new(root.CFrame.X - 4,math.random(-40,40),math.random(-40,40))
    game.Debris:AddItem(earthspike,5)
    local tween = game:GetService("TweenService")
    local tweenInfo2 = TweenInfo.new(0.01,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
    local tweenProperty2 = {Orientation = Vector3.new(0,0,0)}
    local tweenRun2 = tween:Create(earthspike,tweenInfo2,tweenProperty2)
    tweenRun2:Play()

end
for i = 1,30 do
    spawn(earthspike)
end
    earthspike.Touched:connect(function(hit)
    local ehum = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
    earthspike.BrickColor = hit.BrickColor
    if not ten then return end
    if ehum and ehum ~= hum then
    ten = false
    ehum:TakeDamage(5)
    earthspike:Destroy()
    wait()
    ten = true
end
end)

end)

Also, error comes up after a couple spikes are spawned Workspace.Moves.EarthStomp:47: attempt to index global 'earthspike' (a function value) After that, the spikes wont spawn anymore

--Line 47
earthspike.Touched:connect(function(hit)
0
Show the EarthStomp script if you want that error to be fixed. chomboghai 2044 — 5y

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You don't need to differentiate each spike by giving them a different name. As long as you can obtain the reference to the spikes, you can control what happens for each individual spike.

You should connect the Touched event for every spike when creating all the spikes.

Also, don't use findFirstChild. Use FindFirstChild instead because the former is deprecated.

local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local spike = ServerStorage:WaitForChild("Spike")
local ten = true

function earthspike(v)
    earthspike = spike:Clone()
    earthspike.Name = "earthspike"
    earthspike.Anchored = true
    earthspike.CanCollide = true
    earthspike.CFrame = ar.CFrame * CFrame.new(workspace.Baseplate.Position.X, math.random(-40, 40), math.random(-40, 40))
    earthspike.Parent = workspace
    game.Debris: AddItem(earthspike, 5)
    local tweenInfo2 = TweenInfo.new(0.01, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
    local tweenProperty2 = {
        Orientation = Vector3.new(0, 0, 0)
    }
    local tweenRun2 = TweenService: Create(earthspike, tweenInfo2, tweenProperty2)
    tweenRun2: Play()

    -- connect the touched event as you create it
    earthspike.Touched:Connect(function(hit)
        local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
        earthspike.BrickColor = hit.BrickColor
        if not ten then return end
        if ehum and ehum ~= hum then
            ten = false
            ehum:TakeDamage(5)
            earthspike:Destroy()
            wait()
            ten = true
        end
    end)
end

for i = 1,30 do
    spawn(earthspike)
end

And for the error you're getting, you need to show line 47 of that script for us to help debug it.

EDIT:

You are getting that error on line 47 because your function name is earthspike. If you mean to reference the variable inside the function, you firstly have to put the Touched code inside of the function because the variable earthspike is local and is scoped only inside the earthspike function.

My script above should fix it, but you should avoid having variable names and function names be the same. I would recommend changing it to CreateEarthSpikes or something of the sort.

Hope this helps! :)

0
It isn't destroying the spikes when they are touched, but other than that its fine DominousSyndicate 41 — 5y
Ad

Answer this question