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

Shop GUI opens once, but when I try again, doesn't work (In ROBLOX Server)?

Asked by 7 years ago

I have a script, whenever someone touches this specific object, it opens a shop GUI. It works in Solo Mode in Roblox Studio, but when I actually go and play it on my Roblox game, it appears once and when I close it and try open it, it does nothing.

local Gui = game.ReplicatedStorage.Bit_Burger

function GiveGui(Player)
    if Player.PlayerGui:FindFirstChild(Gui.Name)~= nil then return end
    Gui:Clone().Parent = Player.PlayerGui 
end

script.Parent.Touched:connect(function(hit)

    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    GiveGui(Player) 

end)

Here is the close local script as well!

script.Parent.MouseButton1Down:connect(function(player)
    -- Delete the players' Gui

    game.Players.LocalPlayer.PlayerGui.Bit_Burger:Destroy()

end)

2 answers

Log in to vote
0
Answered by 7 years ago

The issue you are experiencing is a Client-Server and FilteringEnabled issue. Here's an excerpt from the Roblox Wiki:

"There are some actions that only the server can perform, and others that only the client can. In some cases the server or client will want the other to perform one of these actions. For example, data stores are only accessible on the server, but clients may want to access the information they store. In this case, a RemoteEvent or RemoteFunction can be used to both send a request from the client to the server to get information from a Data Store, and to send a response from the server back to the client."

Here are a few links to Wiki pages that will help you, including the one I quoted from:

ReplicatedStorage

ServerStorage

FilteringEnabled

Remote Events and Functions

I highly recommend reading these Wiki pages and familiarizing yourself with their content

I tested your code in studio play mode, studio test mode, and on an actual Roblox server. I came to the same result that your question states, until I turned FilteringEnabled off. I was then able to successfully copy the GUI to the player, remove it, and repeat several times. I was able to do this in all three testing situations. This therefore leads me to believe that it is a Client-Server issue.

You have the GUI placed inside ReplicatedStorage. This means that it is replicated to the server and all clients connected to the server. Therefore, the client can actually access the GUI in ReplicatedStorage and :Clone() it to their own PlayerGui, instead of the Server doing this. However, you have this event take place upon a player touching a Part in Workspace, which is controlled by the Server.

Therefore you could use a RemoteEvent, that is fired on the Server with the listener on the Client, to tell the Client to copy the GUI from ReplicatedStorage and insert it into PlayerGui.

Also, to prevent having a separate LocalScript in both the GUI and StarterPlayerScripts, you could consolidate to just the single LocalScipt in StarterPlayerScripts, and place the MouseButton1Down function inside the scope of the RemoteEvent listener, after the GUI is cloned.

If this sounds slightly confusing, here are a few examples of what your code might look like:

Server Script within the Part:

local players = game:FindService("Players")
local RepStorage = game.ReplicatedStorage
local RemoteEvent1 = RepStorage.RemoteEvent

script.Parent.Touched:connect(function(hit)
    local player = players:GetPlayerFromCharacter(hit.Parent)
    RemoteEvent1:FireClient(player)
end)

LocalScript within StarterPlayerScripts:

local RepStorage = game.ReplicatedStorage
local RemoteEvent1 = RepStorage.RemoteEvent
local player = game.Players.LocalPlayer
local Gui = RepStorage.Bit_Burger

function GiveGui()
    if player.PlayerGui:FindFirstChild(Gui.Name)~= nil then 
        return 
    end

    Gui:Clone().Parent = player.PlayerGui 
end

RemoteEvent1.OnClientEvent:connect(function()
    GiveGui()
    local copiedGui = player.PlayerGui:FindFirstChild(Gui.Name)

    copiedGui.MYTEXTBUTTONHERE.MouseButton1Down:connect(function()
        -- Delete the players' Gui
        player.PlayerGui.Bit_Burger:Destroy()
    end)
end)

I hope this helps! Remember to study code and learn from it! Copy and pasting never gets anyone anywhere

If this answer has helped you or solved your problem, don't forget to upvote and confirm.

Ad
Log in to vote
-1
Answered by
Tigeon -2
7 years ago

You are completely destroying the GUI when you close it with Destroy()

try making its Visible property false

0
I'm not 100% sure thats an actual fix for it, though Tigeon -2 — 7y
0
No, he's destroying the copy that is inside PlayerGui, not the original inside ReplicatedStorage. SteamDemand 312 — 7y

Answer this question