luaroblox

Unable to pick up cloned object in Roblox Lua. How to fix?


I am working on a Roblox game where I need to clone an object and allow players to pick up the cloned object after it has been thrown. However, I am facing an issue where the cloned object cannot be picked up.

Here is my local script for throwing

local userInputService = game:GetService("UserInputService")
local playersService = game:GetService("Players")
local tool = script.Parent.Parent
local humanoid = playersService.LocalPlayer.Character:WaitForChild("Humanoid")
local workspace = game:GetService("Workspace")

local isEquipped = false
local mouse = playersService.LocalPlayer:GetMouse()

local function createToolCopy()
    local toolCopy = tool:Clone()
    toolCopy.Parent = workspace

    local throwDirection = (mouse.Hit.p - toolCopy.Handle.Position).unit
    local throwForce = 100 

    toolCopy.Handle.Velocity = throwDirection * throwForce
    isEquipped = false
    tool:Destroy()
end


tool.Equipped:Connect(  function() isEquipped = true end)
tool.Unequipped:Connect(function() isEquipped = false end)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if isEquipped and not gameProcessedEvent then
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            createToolCopy()
        end
    end
end)

This is my global script for for picking up the cup

local clickDetector = script.Parent
local tool = clickDetector.Parent

if clickDetector then
    clickDetector.MaxActivationDistance = 10

    clickDetector.MouseClick:Connect(function(playerWhoClicked: Player)
        print("Player clicked the clickDetector")
        tool.Parent = playerWhoClicked.Backpack
    end)
else
    warn("clickDetector not found in the object.")
end

I also provide for a general understanding of the situation, this is struct of cup:

├─ clickDetector/
│  ├─ GlobalScriptForPickUp
│  ├─ LocalScriptForThrowing
├─ ModelOfCup
├─ Handle/
│  ├─ TouchInterest
│  ├─ WeldConstraint

I wanted it to be like this: the player picks up the cup -> picks it up -> right mouse button -> the cup in the inventory is cloned into the workspace -> flies -> disappears from the inventory -> the player runs up to the cloned cup and can throw it again (although in the future I would like to make a system throwing force depending on the length of the press, and if the force is greater than a certain value, then the cup will break)

What happened: All of the above steps were completed, except for the last one, which is why I wrote the post


Solution

  • This is a typical script that would have worked in Filtering Disabled. Though since the introduction of Filtering Enabled only, the server filters what the client can do. So it is now not possible for clients to create serverscripts (in your case global scripts) anymore, as they would be able to affect other clients too.

    Since a localscript and therefore a client creates a new tool that includes a serverscript, the server won't detect that serverscript was made by the client, as it isn't replicated to the server, neither the tool.

    Consider using RemoteEvents to communicate between the client and server.

    I suggest for you to seperate the detection of tool activation on the client and the rest on the sever (e.g. throwing tool and cloning it)