Search code examples
luaroblox

How can i make PlayerGui Code run on a server script?


I am making a game for fun and i want to make a gui that EVERYONE sees, not just me so how do i make EVERYONE see a gui, and not just the local player?

local Player = game:GetService('Players')
local player = Player.LocalPlayer
magic = script.Parent.Parent.Parent.DramaticOPMODE.Top1
magic2 = script.Parent.Parent.Parent.DramaticOPMODE.Top2
script.Parent.MouseButton1Click:Connect(function()
    magic.Position = UDim2.new(0, 0, -1, 0)
    magic2.Position = UDim2.new(0, 0, 2, 0)
    magic2.Indicator.Visible = false 
    magic2.Indicator.Text = "" 
    magic:TweenPosition(UDim2.new(0, 0, 0, 0)) 
    magic2:TweenPosition(UDim2.new(0, 0, 0.748, 0))
    magic2.Indicator.Visible = true 
    wait(3)

i think this is enough code to get the point


Solution

  • GUI code always needs to run in a LocalScript, because they execute on each individual client. But, if you want everyone to see something at the same time, you can use a server Script to fire a signal to all players that they should show the GUI, and you can do that using RemoteEvents.

    RemoteEvents allow communication between the server and clients, and vice versa. So if you want a player to press a button on their GUI, and have everyone see a thing happen, you need the client to fire the remote event up to the server, then have the server fire the event out to all players.

    So, follow these steps...

    1. Create a RemoteEvent in a shared location like ReplicatedStorage, or the Workspace
    2. Create a Script in ServerScriptService or the Workspace that observers and fires the event.
    3. Update your LocalScript to also observe the event, and play the animation when it happens.

    So in your LocalScript...

    -- find the RemoteEvent
    local showTheMagicEvent = game.ReplicatedStorage.RemoteEvent -- whatever you named it
    -- listen for when the server tells us that someone hit the button 
    showTheMagicEvent.OnClientEvent:Connect(function()
        -- play the animation
        local magic = script.Parent.Parent.Parent.DramaticOPMODE.Top1
        local magic2 = script.Parent.Parent.Parent.DramaticOPMODE.Top2
    
        magic.Position = UDim2.new(0, 0, -1, 0)
        magic2.Position = UDim2.new(0, 0, 2, 0)
        magic2.Indicator.Visible = false 
        magic2.Indicator.Text = "" 
        magic:TweenPosition(UDim2.new(0, 0, 0, 0)) 
        magic2:TweenPosition(UDim2.new(0, 0, 0.748, 0))
        magic2.Indicator.Visible = true 
        wait(3)
        --...
    end)
    
    
    -- whenever someone hits this button, tell the server to show everyone
    script.Parent.MouseButton1Click:Connect(function()
        showTheMagicEvent:FireServer()
    end
    

    Then in your server Script...

    -- find the RemoteEvent
    local showTheMagicEvent = game.ReplicatedStorage.RemoteEvent -- whatever you named it
    showTheMagicEvent.OnServerEvent:Connect(function(player)
        -- when this event fires on the server, turn around and fire it back to all the clients
        showTheMagicEvent:FireAllClients()
    end)