Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Filteringenabled - can't access PlayerGui?

Asked by 8 years ago

I'm currently converting my game to use filteringenabled and I ran into a slight problem here. Basically what i'm trying to do is when a player clicks a gui, it will remove the players car from workspace and the cars gui's; which is just some controls for the car; from the player, but the script can't access PlayerGui to remove the car's gui!

here are the scripts:

I have a local script in the gui

script.Parent.MouseButton1Down:connect(function()
    game.Workspace.Events.RemoveCar.Clicked:FireServer()
end)

and a normal script in a folder which is in workspace with a remoteevent called "clicked" in it

script.Clicked.OnServerEvent(function(player)
    local found = false
    for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
        if obj:FindFirstChild("user") then
            if obj.user.Value == player.Name then
                found = true
                obj:remove()
            end
        end
    end
    local gui = player.PlayerGui:FindFirstChild("Display")
    if gui then
        gui:remove()
    end
    if found == false then
        player.PlayerGui.carRemove.TextButton.Text = "You have none!"
        wait(2)
        player.PlayerGui.carRemove.TextButton.Text = "Remove Car"
    end
end)

the problem is, the script can't access the players PlayerGui and I cannot find a way around this!

0
FireClient UniversalDreams 205 — 8y
0
if I use fireclient it's going to say in output that it can only be called from server alex_ander 163 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Instead of removing the GUI from server side, delete it directly from the event in your local script.

Just because changes won't replicate to server, it doesn't mean you can't create objects or destroy them from client side. It's actually very benifitial to do so and it makes sense in this case, since UI has nothing to do with other server, it's completely local feature.

This is how it could look.

The local script:

script.Parent.MouseButton1Down:connect(function()
    game.Workspace.Events.RemoveCar.Clicked:FireServer()

    -- Use same loop in client script, just don't delete the car
    local found = false
    for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
        if obj:FindFirstChild("user") then
            if obj.user.Value == game.Players.LocalPlayer.Name then
                found = true
            end
        end
    end

    local gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("Display")
    if gui then
        -- Use destroy instead of remove. Remove only sets the object's parent to nil, and might not get garbage collected if something is still accessing the object
        gui:Destroy()
    end

    if found == false then
        game.Players.LocalPlayer.PlayerGui.carRemove.TextButton.Text = "You have none!"
        wait(2)
        game.Players.LocalPlayer.PlayerGui.carRemove.TextButton.Text = "Remove Car"
    end
end)

The server script:

script.Clicked.OnServerEvent(function(player)
    -- All the server should do is destroy the car
    for _, obj in pairs(game.Workspace.Vehicles:GetChildren()) do
        if obj:FindFirstChild("user") then
            if obj.user.Value == player.Name then
                obj:Destroy()
            end
        end
    end
end)
Ad

Answer this question