MouseClick 和 MoveTo 事件不适用于产生的第二部分

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

我正在做一个项目,玩家点击一个对象然后它走过去,等一下,然后对象从游戏中移除并在排行榜上更新点数。问题是它只适用于第一轮。第二次,生成了新部件,它确实有 ClickDetector 作为其子部件,但它不起作用。

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

输出中没有错误消息。只是在新生成的部分,"clicked" 没有打印。

这样行吗,你的问题很难理解,不过我想你的意思是你再点一下就不会出现克隆了。这行得通吗?此外,您不需要在循环中包含点击事件,也不需要在每次玩家点击时进行克隆,在点击函数中您可以对 Clone 变量进行克隆。

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)

您的问题是 ClickDetector 中的函数正在使用对 Clone 的引用,当 Clone 被销毁时,它不再存在。如果您的代码只是简单地从世界中取消对象的父级而不是销毁它,您的代码就可以工作。

-- 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

这样一来,你就不用担心花的第二次或第三次产卵了,因为永远只有一朵花。