简体   繁体   English

MouseClick和MoveTo事件在生成的第二部分上不起作用

[英]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. 第二次,新零件产生,它的确将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

There are no error messages on output. 输出上没有错误消息。 Just that on the new spawned part, "clicked" is not printing. 只是在新生成的零件上,“ clicked”未打印。

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. 您的问题是ClickDetector中的函数正在使用对Clone的引用,并且销毁Clone时,该功能将不再存在。 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. 另外,您无需在循环内部包含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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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