Hello,
I’m creating a script that shows you a gui when you encounter some peculair parts in a certain distance. However it keeps cloning the gui even though I’m checking if there’s already a gui that has been cloned. It also prints Succesfully cloned gui! infinite times
Here’s the script.
local gui = game.ReplicatedFirst:WaitForChild("Epickup")
local canClone = false
while true do wait()
local object = findClosestObject()
if object == nil then --Check if we exceeded the distance between the obkect and the RootPart. If so, check for any gui's that's named Epickup and destroy them
for _, v in pairs(playerGui:GetChildren()) do
if v.Name == "Epickup" then
v:Destroy()
end
end
else --Clone the gui
for _, v in pairs(playerGui:GetChildren()) do --Checking if the gui hasn't been cloned before
if v.Name ~= "Epickup" then
canClone = true
if canClone then
canClone = false
gui:Clone().Parent = playerGui
print("Succesfully cloned gui!")
canClone = true
break
end
end
end
end
end
Thanks for your reply. Could you elaborate this a bit more? I’ve never used “Continue”. I don’t know how I should use that in this piece of code. And should I delete the canClone entirely?
Edit: In this piece of code, using break will stop the while wait() do. Continue will just continue the code.
Not 100% sure, you can try continue and break. I have forgotten the difference as I just use continue constantly now.
Don’t remember the difference. Pretty sure break will do a better job than continue.
local gui = game.ReplicatedFirst:WaitForChild("Epickup")
while true do
wait()
local object = findClosestObject()
if object == nil then --Check if we exceeded the distance between the obkect and the RootPart. If so, check for any gui's that's named Epickup and destroy them
if playerGui:FindFirstChild("Epickup") then --Will continue if it finds Epickup
playerGui.Epickup:Destroy()
end
else
if not playerGui:FindFirstChild("Epickup") then --Will continue if there isn't a Epickup
gui:Clone().Parent = playerGui
print("Succesfully cloned gui!")
end
end
end
What FindFirstChild does is if it finds an object with that name, it will return that instance but if there is no object with that name it will return nil.