Ad
Log in to vote
0

Cloned parts not firing click event?

Asked by 5 days ago
Edited 5 days ago

I have a game that has 2 cubes, a cube with a "2" on it and a cube with a "4" on it. If you click the cube with a "2", something will happen but if you click a cube with a "4" on it, something will happen AND the "4" cube will be replaced with a "2" cube. That new "2" cube should act like all the other "2" cubes that were in the game when I ran it. Everything works fine except the "2" cube that came from the "4" Cube, those do not fire the click event like the other "2" Cubes do.

Here is my workspace: https://imgur.com/a/kZQODLm I have a local script in playerGUI (this detects when a player clicks on a cube and sends it to a remote event) The remote event is stored in replicated storage. Finally, I have a server scipts in ServerScriptService that handles what happens when it gets a signal from the remote event. The server script does fire for all cubes except the newly created ones that came from the "4" cube.

Here is the code for my local script in player GUI:

local twoFolder = game.Workspace.TwoCubes
local fourFolder = game.Workspace.FourCubes

for i, v in next, twoFolder:GetChildren() do
    v.ClickDetector.MouseClick:Connect(function()
        local target = game.Players.LocalPlayer:GetMouse().Target
        script.click_sound:Play()
        game.ReplicatedStorage.Cube2Clicked:FireServer(target)
    end)
end

for i, v in next, fourFolder:GetChildren() do
    v.ClickDetector.MouseClick:Connect(function()
        local target = game.Players.LocalPlayer:GetMouse().Target
        script.click_sound:Play()
        game.ReplicatedStorage.Cube4Clicked:FireServer(target)
    end)
end

And here is my code for the server script in server script service:

local TwoCubeSS = game.ServerStorage.TwoCube


game.ReplicatedStorage.Cube2Clicked.OnServerEvent:Connect(function(player, part)
    print(player, "has clicked the 2 cube (",part,")")
end)

game.ReplicatedStorage.Cube4Clicked.OnServerEvent:Connect(function(player, part)
    local newCube = TwoCubeSS:Clone()
    newCube.Parent = workspace.TwoCubes
    part:Destroy()
    print(player, "has clicked the 4 cube! (",part,")")
end)

Answer this question