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

Gear Shop GUI|Purchasing Tools In-Game Doesn't Work. Someone Explain?

Asked by 6 years ago
Edited 6 years ago

I have made a GUI Gear Shop for a my game, and so far added just a gravity coil, which is put in lighting for the script. I tested it out in studio and it worked greatly, then I published and tested it out in a game. But even though I had the right amount of currency it wouldn't put the item into my inventory. Could someone explain what is wrong? Heres the script.

script.Parent.MouseButton1Click:connect(function() local Lighting = game:GetService("Lighting") local item = Lighting:WaitForChild("GravityCoil") local price = 50 local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats")

if stats.Money.Value >= price then stats.Money.Value = stats.Money.Value - price local cloned = item:Clone() local cloned2 = item:Clone() cloned2.Parent = player.Backpack cloned.Parent = player.StarterGear end end)

0
Why is your tool in lighting? Using Lighting as storage is in no way supported by ROBLOX, FYI. User#19524 175 — 6y
0
Please use code blocks. User#19524 175 — 6y
0
make this a local script and add wait(5) to the start greatneil80 2647 — 6y
0
Why would they wait 5 seconds User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

When you test in studio, everything runs locally. This is why LocalScripts may have access to server-side only services, such as ServerStorage or ServerScriptService. There is no separation between the server and client. It all runs on the client.

When you play in a local/real server, the separation comes in. But with FilteringEnabled, they're separated even more, and this separation is much more apparent.

Your current code does what it does, but the changes don't replicate, most likely because your game is FilteringEnabled, and everything is being done by the client. You should handle the cloning to the server. Although technically it would be bad to check the requirements (such as checking if player has enough money) on the client, you don't want to overwhelm the server with all of this checking. Use a RemoteFunction to get the price of the GravityCoil, and a RemoteEvent to handle the cloning.

--LocalScript, inside the same place you have it

local rep = game:GetService"ReplicatedStorage"
local plr = game:GetService("Players").LocalPlayer
local price = rep:WaitForChild("GetPrice"):InvokeServer"GravityCoil"
local giveTool = rep:WaitForChild"GiveTool"

script.Parent.MouseButton1Click:Connect(function()
    if plr.leaderstats.Money.Value >= price then
        giveTool:FireServer"GravityCoil"
    end
end)

Server script coding;

--Server Script
local rep = game:GetService"ReplicatedStorage"
local getPrice = rep:WaitForChild"GetPrice"
local giveTool = rep:WaitForChild"GiveTool"

giveTool.OnServerEvent:Connect(function(player, toolName)
    rep[toolName]:Clone().Parent = player.Backpack
    rep[toolName]:Clone().Parent = player.StarterGear
    --Cloning handled here
end)

local GetToolPrice

GetToolPrice = function(player, toolName)
    return rep[toolName].Price.Value 
    --I inserted an IntValue inside the tool called Price. 
end

getPrice.OnServerInvoke = GetToolPrice

On a side note, don't use Lighting as storage, as this is in no way supported by ROBLOX. Learn more here.

Ad

Answer this question