I’m making a merging game and i want that if 2 spheres of the same lvl (attribute) touches eachothers, they both delete themselves to spawn a higher level sphere. (Inspired of Merge!).
The issue is, since I use a system that it detects when it touches another sphere and checks if it’s the good one, if it is, it then spawns a new sphere. This all works until since the detecting system works on both spheres, it spawns not 1 higher level sphere, but 2.
I haven’t tried any solutions, as I do not know how to fix the problem or neither know how to search this up (how to word my problem and potentially found someone else having the same problem).
I also have a problem where newly created spheres can’t merge, i tried turning the Sphere.Touched function into a local function and using Sphere.Touched AND NewSphere.Touched but since NewSphere was in the function i can’t use it.
Any help is appreciated, here’s my code :
local SSS, MaxSpawnLVL = workspace.Values.SphereSpawnSpeed.Value, workspace.Values.MaxSpawnLVL.Value
while true do
wait(SSS)
local Sphere = Instance.new("Part")
Sphere.Shape = Enum.PartType.Ball
Sphere:SetAttribute("Level", math.random(1, MaxSpawnLVL))
local SizeLVLRatio = 4 + Sphere:GetAttribute("Level")
Sphere.Size = Vector3.new(SizeLVLRatio, SizeLVLRatio, SizeLVLRatio)
local BSize = workspace.Baseplate.Size.X / 3 - 10
Sphere.Position = Vector3.new(math.random(0 - BSize, BSize), 52, math.random(0 - BSize, BSize))
Sphere.Massless = true
Sphere.TopSurface, Sphere.BottomSurface = "Smooth", "Smooth"
Sphere.Parent = workspace.SphereHolder
Sphere.Touched:Connect(function(hit)
local HA = hit:GetAttribute("Level")
if HA == Sphere:GetAttribute("Level") then
local NewSphere = Instance.new("Part")
NewSphere.CFrame = CFrame.new(Sphere.CFrame.X, Sphere.CFrame.Y + 10, Sphere.CFrame.Z)
NewSphere:SetAttribute("Level", Sphere:GetAttribute("Level") + 1)
local SizeLVLRatio = 4 + NewSphere:GetAttribute("Level")
NewSphere.Size = Vector3.new(SizeLVLRatio, SizeLVLRatio, SizeLVLRatio)
NewSphere.Shape = Enum.PartType.Ball
NewSphere.TopSurface, NewSphere.BottomSurface = "Smooth", "Smooth"
NewSphere.Massless = true
NewSphere.Parent = workspace.SphereHolder
Sphere:Destroy()
hit:Destroy()
local Sphere = NewSphere
print("Touching test")
end
end)