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.

01local Gui = game.ReplicatedStorage.Bit_Burger
02 
03function GiveGui(Player)
04    if Player.PlayerGui:FindFirstChild(Gui.Name)~= nil then return end
05    Gui:Clone().Parent = Player.PlayerGui
06end
07 
08script.Parent.Touched:connect(function(hit)
09 
10    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
11    GiveGui(Player)
12 
13end)

Here is the close local script as well!

1script.Parent.MouseButton1Down:connect(function(player)
2    -- Delete the players' Gui
3 
4    game.Players.LocalPlayer.PlayerGui.Bit_Burger:Destroy()
5 
6end)

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:

1local players = game:FindService("Players")
2local RepStorage = game.ReplicatedStorage
3local RemoteEvent1 = RepStorage.RemoteEvent
4 
5script.Parent.Touched:connect(function(hit)
6    local player = players:GetPlayerFromCharacter(hit.Parent)
7    RemoteEvent1:FireClient(player)
8end)

LocalScript within StarterPlayerScripts:

01local RepStorage = game.ReplicatedStorage
02local RemoteEvent1 = RepStorage.RemoteEvent
03local player = game.Players.LocalPlayer
04local Gui = RepStorage.Bit_Burger
05 
06function GiveGui()
07    if player.PlayerGui:FindFirstChild(Gui.Name)~= nil then
08        return
09    end
10 
11    Gui:Clone().Parent = player.PlayerGui
12end
13 
14RemoteEvent1.OnClientEvent:connect(function()
15    GiveGui()
View all 22 lines...

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