I am trying to make a script that creates three “patches”, put them in some random positions, and wait until the player touches all three of them, only to repeat the cycle. However, there are backup positions that are set if some patches are in the same position. However, these patches always resort to the backup positions, and I do not have a clue why.
Script:
--Variables
local serverStorage = game.ServerStorage
local patch = serverStorage.Patch
local y = 5.25
local patchesHit = 0
local debounce1 = false
local debounce2 = false
local debounce3 = false
local backupPos1 = Vector3.new(-1156.5, 5.5, -176.5)
local backupPos2 = Vector3.new(-1156.5, 5.5, -163.5)
local backupPos3 = Vector3.new(-1178.5, 5.5, -168.5)
--Patches stuff
while true do
--Generate position coordinates
local x1 = math.random(-1159, -1176)
local z1 = math.random(-179, -162)
local x2 = math.random(-1159, -1176)
local z2 = math.random(-179, -162)
local x3 = math.random(-1159, -1176)
local z3 = math.random(-179, -162)
--Make 3 patches
local patch1 = patch:Clone()
local patch2 = patch:Clone()
local patch3 = patch:Clone()
--Position the patches
patch1.Position = Vector3.new(x1, y, z1)
patch2.Position = Vector3.new(x2, y, z2)
patch3.Position = Vector3.new(x3, y, z3)
--Make sure they aren't in the same spot
if (patch1.Position == patch2.Position or patch3.Position) then
patch1.Position = backupPos1
end
if (patch2.Position == patch1.Position or patch3.Position) then
patch2.Position = backupPos2
end
if (patch3.Position == patch1.Position or patch2.Position) then
patch3.Position = backupPos3
end
--Parent the patches to workspace
patch1.Parent = game.Workspace
wait(.5)
patch2.Parent = game.Workspace
wait(.5)
patch3.Parent = game.Workspace
--Patches touch events
patch1.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not debounce1 and humanoid then
debounce1 = true
patch1:Destroy()
patchesHit = patchesHit + 1
print(patchesHit)
wait(.1)
debounce1 = false
end
end)
patch2.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not debounce2 and humanoid then
debounce2 = true
patch2:Destroy()
patchesHit = patchesHit + 1
print(patchesHit)
wait(.1)
debounce2 = false
end
end)
patch3.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not debounce3 and humanoid then
debounce3 = true
patch3:Destroy()
patchesHit = patchesHit + 1
print(patchesHit)
wait(.1)
debounce3 = false
end
end)
--Wait until the 3 patches are hit
repeat
wait(.01)
until patchesHit == 3
print("patched!")
patchesHit = 0
wait(2.5)
end
-
What do you want to achieve? I want to make a script that makes 3 patches in random positions, waits until the patches are touched, then resets the patches again.
-
What is the issue? The patches always position themselves into the backup positions.
-
What solutions have you tried so far? I tried searching for similar topics about my issue, but I found no posts with an answer.
I figured that something about the if statements are off, but I don’t think there’s anything wrong with it. I know I change the patches’ positions, but it should only do this if one of them were in the same position as another.