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

How do i stop Duplication of Items in the Inventory?

Asked by 3 years ago

Hello Fellow Programmers!

So, First, All the events and Items are kept in ReplicatedStorage where they both can be seen by the server and the Client.

So, I decided to make a GUI Shop Button where a person selects an Item and Gets the following Details

I made a Module script for the buttons so that I don't use the same functions over and over again

And then I made a Server script called ItemTransaction where the Process takes place

But whenever I buy a second item, the first item always duplicates.

So here is the Code

MODULE SCRIPT

local replicatedStorage = game:GetService("ReplicatedStorage")
local PurchasingEvent = replicatedStorage.Events.PurchasingEvent
local player = game.Players.LocalPlayer
local module = {}

module.DisplayFunc = function(ItemName,Description,Price)
    local DisplayFrame = game.Players.LocalPlayer.PlayerGui.GameShopGui.DisplayFrame
    DisplayFrame.DisplayText.Text = Description
    DisplayFrame.Price.Text = Price
    DisplayFrame.BuyButton.MouseButton1Click:Connect(function()
        PurchasingEvent:FireServer(ItemName,Price)
    end)
end


return module

GUI BUTTON CODE

DisplayModule = require(game.ReplicatedStorage.Module.ModuleScript)
local ItemName = "Grenade"
local Button = script.Parent
local Price = 50
local Description = "KABOOOOOM!!"


Button.MouseButton1Click:Connect(function()
    DisplayModule.DisplayFunc(ItemName,Description,Price)
end)

MAIN SERVER SCRIPT

local replicatedStorage = game:GetService("ReplicatedStorage")
local PurchasingEvent = replicatedStorage.Events.PurchasingEvent
local InsufficientMoneyEvent = replicatedStorage.Events.InsufficientMoneyEvent
local OwnedEvent = replicatedStorage.Events.OwnedEvent

PurchasingEvent.OnServerEvent:Connect(function(player,ItemName,Price)
    local tool = replicatedStorage.Items:FindFirstChild(ItemName)
    if tool then
        if player.leaderstats.Gold.Value >= Price then
            print(ItemName)
            local Item = tool:Clone()
            Item.Parent = player.Backpack
            player.leaderstats.Gold.Value -= Price
            Item = nil
        else
            InsufficientMoneyEvent:FireClient(player)
        end
    else
        print("Error")
    end
end)

If you know where is the error, please tell me.

Thank you very much for listening.

1 answer

Log in to vote
0
Answered by 3 years ago

You can check if there is an item with the same name in the player's Backpack or character before cloning the tool.

Ad

Answer this question