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

时间:2019-05-29 15:27:09

标签: lua roblox

我正在研究一个项目,玩家单击该对象,然后将其移到该对象上,等待一秒钟,然后将该对象从游戏中移除,并在排行榜上更新积分。问题在于它仅在第一轮比赛中有效。第二次,生成新零件,并且确实将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”没有打印。

2 个答案:

答案 0 :(得分:1)

您的问题是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

这样,您不必担心花朵的第二或第三次产卵,因为永远只有一朵花。

答案 1 :(得分:0)

这项工作会很难理解您的问题,但是我认为您的意思是,再次单击该克隆后将不会出现。这项工作做得好吗?另外,您无需在循环内添加click事件,也无需在玩家每次单击时进行克隆,而在click函数内就可以克隆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)