Search code examples
luaroblox

Other players can not see an object duplicate


I have this script that duplicates an object and teleports it to the player when the GUI button is pressed (You can think of GMod to make things easier). It is stored in a LocalScript inside the button and when you press it, but it's only visible for the player that clicked the button.

I'm not exactly sure how I would solve the problem or what the problem is, but I think it's because it's all stored into a LocalScript. I'm new to Lua and Roblox game development and I didn't really take any lessons on it, I'm just working from my memory and experience. Any and all suggestions are greatly appreciated. Also, if I need to give more information, please ask and I will provide it.

My script:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    local rag = game.Lighting.ragdolls_presets["Wood Crate"]:Clone()
    local X = player.Character.UpperTorso.Position.X 
    local Y = player.Character.UpperTorso.Position.Y + 10
    local Z = player.Character.UpperTorso.Position.Z
    
    rag.Parent = game.Workspace.ragdolls
    
    
    local children = rag:GetChildren()
    
    for i = 1, #children do
        local child = children[i]
        child.Position = Vector3.new(X,Y,Z)
    end
     
end)

Thank you in advance!


Solution

  • When you clone something in a LocalScript, it only clones on the client side. Changes done on the client side never gets replicated to the server side, however all changes done on the server side would get replicated to all clients, which are the players. So to fix this you'd need to use RemoteEvents, which is a way for the client to tell the server to do something.

    So instead of doing this in a LocalScript

    local player = game.Players.LocalPlayer
    
    script.Parent.MouseButton1Click:Connect(function()
        local rag = game.Lighting.ragdolls_presets["Wood Crate"]:Clone()
        local X = player.Character.UpperTorso.Position.X 
        local Y = player.Character.UpperTorso.Position.Y + 10
        local Z = player.Character.UpperTorso.Position.Z
        
        rag.Parent = game.Workspace.ragdolls
        
        
        local children = rag:GetChildren()
        
        for i = 1, #children do
            local child = children[i]
            child.Position = Vector3.new(X,Y,Z)
        end
         
    end)
    

    Put a new RemoteEvent in game.Replicated Storage, then: This should be the LocalScript

    local Event = game.ReplicatedStorage.RemoteEvent
    
    script.Parent.MouseButton1Click:Connect(function()
        Event:Fire()
    end)
    

    And this should be the ServerScript (or Script for short)

    local Event = game.ReplicatedStorage.RemoteEvent
    Event.OnServerEvent:Connect(function(player) 
        local rag = game.Lighting.ragdolls_presets["Wood Crate"]:Clone()
        local X = player.Character.UpperTorso.Position.X 
        local Y = player.Character.UpperTorso.Position.Y + 10
        local Z = player.Character.UpperTorso.Position.Z
    
        rag.Parent = game.Workspace.ragdolls
    
        local children = rag:GetChildren()
    
        for i = 1, #children do
            local child = children[i]
            child.Position = Vector3.new(X,Y,Z)
        end
    end)
    

    Also, it is very important to do server-side validation when using RemoteEvents, for example: instead of checking whether a player has enough money to spawn something in a LocalScript before the RemoteEvent is fired, it should be done in both LocalScript and the ServerScript. The cause for this is that players can change anything in their client side, so information from the client can NOT be trusted. Optionally you can also have time-outs in each side because, lets say a player has millions of money, he can keep executing the RemoteEvent hundreds of times in under a minute which can crash the server or cause extreme lag. So a 3 second - 1 minute time out is sometimes necessary

    More information about Roblox's Client-Server Model: https://create.roblox.com/docs/scripting/networking/client-server-model

    More information about Roblox RemoteEvents: https://create.roblox.com/docs/reference/engine/classes/RemoteEvent