简体   繁体   中英

MouseClick and MoveTo event is not working on the 2nd Part that spawned

I'm working on a project that the player clicks on an object and it walks over to it, wait a second, then the object is removed from the game and points update on the leaderboard. The problem is it only works on the first round. The 2nd time, the new part spawned and it does have the ClickDetector as its child, but it does not function.

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

local spawners = workspace.Spawners.level1:GetChildren()
local spawnSelection = math.random(1,#spawners)
local spawner = spawners[spawnSelection]

while true do

    wait(1)
    if Clone.Parent == nil then
        Clone.Parent = workspace.Flowers.level1
        Clone.CFrame = spawner.CFrame + Vector3.new(math.random(-5,5),1,math.random(-5,5))
        print("Clone added")

        ClickDetector.MouseClick:Connect(function(playerWhoClicked)
            playerWhoClicked.Character.Humanoid:MoveTo(Clone.Position,Clone)
            print("clicked")

            wait(1)

            Clone:Remove()
            print("Clone removed")

            local flowerValue = playerWhoClicked.leaderstats.Flowers
            local coinsValue = playerWhoClicked.leaderstats.Coins
            flowerValue.Value = flowerValue.Value + 1
            coinsValue.Value = coinsValue.Value + 5
        end)
    end

end

There are no error messages on output. Just that on the new spawned part, "clicked" is not printing.

Your problem is that the function in the ClickDetector is using a reference to Clone, and when Clone is destroyed, it doesn't exist anymore. Your code would work if it simply unparented the object from the world rather than destroyed it.

-- choose a random flower and clone it
local flowers = game.ReplicatedStorage.Flowers.level1:GetChildren()
local selection = math.random(1,#flowers)
local Clone = flowers[selection]:Clone()

-- configure a click detector into the cloned flower
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = Clone
ClickDetector.MaxActivationDistance = 500
ClickDetector.MouseClick:Connect(function(playerWhoClicked)
    -- when a player clicks on the flower, move the player over to it
    playerWhoClicked.Character.Humanoid:MoveTo(Clone.Position,Clone)
    print("clicked")

    -- remove the cloned flower from the workspace, but don't destroy it
    wait(1)
    Clone.Parent = nil -- << simply hide it from the world

    -- award the player with some points
    local flowerValue = playerWhoClicked.leaderstats.Flowers
    local coinsValue = playerWhoClicked.leaderstats.Coins
    flowerValue.Value = flowerValue.Value + 1
    coinsValue.Value = coinsValue.Value + 5
end)

-- choose a random spawn location
local spawners = workspace.Spawners.level1:GetChildren()
local spawnSelection = math.random(1,#spawners)
local spawner = spawners[spawnSelection]

-- begin a loop to place the flower into the world
while true do

    wait(1)
    -- if the flower isn't visible, place it near a specific location
    if Clone.Parent == nil then
        Clone.Parent = workspace.Flowers.level1
        Clone.CFrame = spawner.CFrame + Vector3.new(math.random(-5,5),1,math.random(-5,5))
        print("Clone added into the world")

        -- now wait for a player to click on it and unparent it.
        -- this case will come back around a second later, and it will be added back in.
    end

end

This way, you don't have to worry about the second or third spawning of the flower, because there will only ever be one flower.

Would this work, it was hard to understand your question, but I think what your saying is that the clone wont appear after you click again. Well did this work? Also, you don't need to have the click event inside the loop, and to make a clone every time the player clicks, inside the click function you make a clone of the Clone variable.

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

local spawners = workspace.Spawners.level1:GetChildren()
local spawnSelection = math.random(1,#spawners)
local spawner = spawners[spawnSelection]

while true do

    wait(1)
    if Clone.Parent == nil then
        Clone.Parent = workspace.Flowers.level1
        Clone.CFrame = spawner.CFrame + Vector3.new(math.random(-5,5),1,math.random(-5,5))
        print("Clone added")
    end

end
ClickDetector.MouseClick:Connect(function(playerWhoClicked)
    _clone = Clone:Clone()
    playerWhoClicked.Character.Humanoid:MoveTo(_clone.Position,_clone)
    print("clicked")

    wait(1)

    _clone:Destroy()
    print("_clone removed")

    local flowerValue = playerWhoClicked.leaderstats.Flowers
    local coinsValue = playerWhoClicked.leaderstats.Coins
    flowerValue.Value = flowerValue.Value + 1
    coinsValue.Value = coinsValue.Value + 5
end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM