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

This dialog shop im making only works in studio. Do you guys have any pointers?

Asked by 6 years ago

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)

0
Server script or localscript? User#19524 175 — 6y
0
serverscript. Also the items are in serverstorage. I was thinking of moving them to replicatedStorage but then exploiters can get to the items, right? krustatiion -3 — 6y
1
You shouldn’t handle GUI in the server. User#19524 175 — 6y
0
I have a local script within the GUI that makes them vanish after 3 seconds. The server only handles making them appear in the playerGui krustatiion -3 — 6y
View all comments (2 more)
1
Thats still handling GUI. The server can’t access PlayerGui/shouldn’t be accessing PlayerGui. Handling GUI on the server presents weird behaviour. User#19524 175 — 6y
0
Ok, I didnt know that, but what can i do? thanks. krustatiion -3 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Why Does Your Code Work In Studio And Not In-Game?

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.

You Are Handling Something That Is Meant To Be In The Client In The Server

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.

How To Make Your Code Compatible To Filtering Enabled

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

FireClient() takes one argument, the player. The rest of its arguments are the arguments in the OnClientEvent

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)
0
thank you so much! krustatiion -3 — 6y
0
Your welcome! saSlol2436 716 — 6y
Ad

Answer this question