UPDATE: Solved by instead of Remove() the part, setting its Parent = nil works.
When the player clicks on a part, it will walk to that part and get its leaderstat update. When the part is removed and spawned a new one, the player is not walking over to the new part when clicking it. The new part does have the ClickDetector in it. It works for the first initial spawn only. Can you help me see what's wrong here?
local flowers = game.ReplicatedStorage.Flowers.level1:GetChildren() local selection = math.random(1,#flowers) local Clone = flowers[selection]:Clone() local ClickDetector = Instance.new("ClickDetector") ClickDetector.Parent = Clone ClickDetector.MaxActivationDistance = 500 while true do wait(1) if Clone.Parent == nil then Clone.Parent = workspace.Flowers.level1 local spawners = workspace.Spawners.level1:GetChildren() local spawnSelection = math.random(1,#spawners) local spawner = spawners[spawnSelection] Clone.CFrame = spawner.CFrame + Vector3.new(math.random(-5,5),1,math.random(-5,5)) ClickDetector.MouseClick:Connect(function(playerWhoClicked) local target = Clone playerWhoClicked.Character.Humanoid:MoveTo(target.Position,target) wait(1) target:Remove() local flowerValue = playerWhoClicked.leaderstats.Flowers local coinsValue = playerWhoClicked.leaderstats.Coins flowerValue.Value = flowerValue.Value + 1 coinsValue.Value = coinsValue.Value + 5 end) end end
Since I cannot find the problem in this script yet, I just make an alternative script by putting the part in Workspace instead of cloning it from ReplicatedStorage. And instead of removing it, I just alter its Transparency and debounce then relocating it to another random position before reappearing again.