im trying to do a dialog shop so that it checks if you have enough cash, it gives you the item and shows a success gui, if you doesnt it shows a gui saying not enough. This only works in studio. The code is very self-explanatory. Any help?
script.Parent.DialogChoiceSelected:Connect(function(player, choice) if choice == script.Parent.DialogChoice.FireSword.yes then --if they choose the fire sword if player.leaderstats["Monie'z"].Value >= 100 then player.leaderstats["Monie'z"].Value = player.leaderstats["Monie'z"].Value - 100 local cloneSword = game.ServerStorage.FireSword:Clone() cloneSword.Parent = player.Backpack local guiClone = game.ServerStorage.Success:Clone() guiClone.Parent = player.PlayerGui guiClone.LocalScript.Disabled = false else local guiClone2 = game.ServerStorage.Declined:Clone() guiClone2.Parent = player.PlayerGui guiClone2.LocalScript.Disabled = false end --if they choose the rainbow sword elseif choice == script.Parent.DialogChoice.RainbowSword.yes then if player.leaderstats["Monie'z"].Value >= 50 then player.leaderstats["Monie'z"].Value = player.leaderstats["Monie'z"].Value - 50 local cloneSword2 = game.ServerStorage.RainbowSword:Clone() cloneSword2.Parent = player.Backpack local guiClone = game.ServerStorage.Success:Clone() guiClone.Parent = player.PlayerGui guiClone.LocalScript.Disabled = false else local guiClone2 = game.ServerStorage.Declined:Clone() guiClone2.Parent = player.PlayerGui guiClone2.LocalScript.Disabled = false end end end)
Your code only works in Studio because it follows the way Experimental mode works even though you have enabled Filtering Enabled. Instead of doing playing, try making a Local Server with two players or do team test to test out your code.
Now, the problem with Filtering Enabled is that the server blocks everything the client gives because it is assuming the client is an exploiter. To let the server know that the client isn't an exploiter, we can use RemoteEvents and RemoteFunctions.
To make your code compatible to filtering enabled, we need to use RemoteEvents. Since we are doing changes to the client, we will use :FireClient()
and OnClientEvent
. We will also check if the player has enough money on the Server(so the exploiter can't change how much money the player needs to give/pay).
NOTE: You need to put your GUI's in ReplicatedStorage
since the Client need to access it and the Client can't access the Server Storage's contents.
We need to put 2 RemoteEvents in ReplicatedStorage so both the Client and Server can access it. The first one will be called "SuccessRemoteEvent" and the other will be called "DeclinedRemoteEvent".
The client part of both of the remote events will be a Local Script
This will be our first client code.
local players = game:GetService("Players") local player = players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local repStorage = game:GetService("ReplicatedStorage") local successRemote = repStorage:WaitForChild("SuccessRemoteEvent") local successGui = repStorage:WaitForChild("Success") successRemote.OnClientEvent:Connect(function() local guiClone = successGui:Clone() guiClone.Parent = playerGui end)
What we are doing is we are making the successRemote listen to the OnClientEvent so in our ServerScript, you can use :FireClient()
or :FireAllClients()
. But since you want the GUI to only show for one player, you will use:FireClient()
We will now make our second client code
local players = game:GetService("Players") local player = players.LocalPlayer local playerGui = player:WaitForChild("PlayerGui") local repStorage = game:GetService("ReplicatedStorage") local declinedRemote = repStorage:WaitForChild("DeclinedRemoteEvent") local declinedGui = repStorage:WaitForChild("Declined") declinedRemote.OnClientEvent:Connect(function() local guiClone = declinedGui:Clone() guiClone.Parent = playerGui end)
Okay, now, we need to do the communication between the client and server. This is where :FireClient()
will be handy
Let's make our Server Code
local repStorage = game:GetService("ReplicatedStorage") local successRemote = repStorage:WaitForChild("SuccessRemoteEvent") local declinedRemote = repStorage:WaitForChild("DeclinedRemoteEvent") script.Parent.DialogChoiceSelected:Connect(function(player, choice) if choice == script.Parent.DialogChoice.FireSword.yes then --if they choose the fire sword if player.leaderstats["Monie'z"].Value >= 100 then successRemote:FireClient(player) else declinedRemote:FireClient(player) end --if they choose the rainbow sword elseif choice == script.Parent.DialogChoice.RainbowSword.yes then if player.leaderstats["Monie'z"].Value >= 50 then player.leaderstats["Monie'z"].Value = player.leaderstats["Monie'z"].Value - 50 local cloneSword2 = game.ServerStorage.RainbowSword:Clone() cloneSword2.Parent = player.Backpack successRemote:FireClient(player) else declinedRemote:FireClient(player) end end end)