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)

1 comment
Show the EarthStomp script if you want that error to be fixed.