Hello Devforum! So recently i made an experimental project. So it is similar to a Backpack shop. I only made it to the price reduction. However it doesn’t reduce the price when i click the “buy” button. I have 3 scripts. They’re a module script, a serverscript and a localscript. Here is my code.
--Local script
local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local Shared = RS:WaitForChild("Shared")
local ss = Shared:WaitForChild("Storages")
local storage = ss:GetChildren()
local selectedtemp = nil
local ssg = script.Parent
local ssFrame = script.Parent:WaitForChild("Capacity_frame")
local scrollingFrame = ssFrame:WaitForChild("ScrollingFrame")
local temp = scrollingFrame.temp
local shop = script.Parent.Capacity_frame
local buy = shop:WaitForChild("canbuy")
local framess = ssg:WaitForChild("Frame")
local status = shop:WaitForChild("status")
local function update(toolName, state) -- Added a local function for an more appealing local script
local result = game.ReplicatedStorage.Remotes.buytool:InvokeServer(toolName, state)
print(result)
if result == "Equipped" then
status.Text = "Equipped"
elseif result == "Yes" then
status.Text = "Equipped"
elseif result == "No" then
status.Text = "BUY!"
framess:TweenSize(UDim2.new(0.279, 0,0.1, 0) ,
"Out" ,
"Elastic" ,
1
)
wait(1)
framess:TweenSize(UDim2.new(0, 0,0, 0) ,
"Out" ,
"Elastic" ,
1
)
elseif result == "Owned" then
status.Text = "Equip"
elseif result == "Not Owned" then
status.Text = "BUY!"
end
print("Working")
end
for i, item in ipairs(storage) do
if item:IsA("Folder") then
local price = item:FindFirstChild("Price")
local img = item:FindFirstChild("icon")
local order = item:FindFirstChild("orderst")
if price and img and order then
local tempClone = temp:Clone()
tempClone.Parent = scrollingFrame
tempClone.Name = item.Name
tempClone.img.Image = img.Value
tempClone.LayoutOrder = order.Value
local priceval = Instance.new("StringValue")
priceval.Value = price.Value
priceval.Parent = tempClone
tempClone.Visible = true
tempClone.MouseButton1Click:Connect(function()
shop["tool_img"].Image = img.Value
selectedtemp = tempClone.Name
shop.price.Text = price.Value
shop.name.Text = item.Name
update(tempClone.Name, true) --Starts the Check for an Updated Status (Tool Name, State (False = Purchase, True = Update Label))
end)
else
continue
end
end
end
buy.MouseButton1Click:Connect(function()
print(selectedtemp)
if selectedtemp ~= nil then
update(selectedtemp, false) --Starts the Purchasing Process
end
end)
--module script
local remotecon = {}
local plr = game.Players.LocalPlayer
local items = game.ReplicatedStorage.Shared:WaitForChild("Tools")
local storage = game.ReplicatedStorage.Shared:WaitForChild("Storages")
function remotecon.buy(player, toolName, update)
local tool = items:FindFirstChild(toolName)
if tool ~= nil then
if update == false then -- Update Varible
if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
local price = tool.Cost.Value
--plr dosent have tool
if player.leaderstats.Coins.Value >= price then
print("A")
local newtoolval = Instance.new("StringValue")
newtoolval.Name = toolName
newtoolval.Parent = player
player.Equipped.Value = toolName
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
local gear = tool:Clone()
gear.Parent = player.Backpack
local gear2 = tool:Clone()
gear2.Parent = player.Ownedtools
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
return "Yes"
else
return "No"
end
elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
local gear = tool:Clone()
gear.Parent = player.Backpack
player.Equipped.Value = toolName
return "Equipped"
elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
local gear = tool:Clone()
gear.Parent = player.Backpack
player.Equipped.Value = toolName
return "Equipped"
end
else
if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
return "Not Owned" -- returns Buy and Not Owned
elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
return "Owned" -- returns Owned
elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then
return "Equipped" -- returns Equipped
end
end
else
return "error"
end
end
function remotecon.buyst(player, storagen, update)
local storagef = storage:FindFirstChild(storagen)
if storagef ~= nil then
if update == false then -- Update Varible
if not player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value ~= storagen then
local price = storagef.Cost. Value
--plr dosent have tool
if player.leaderstats.Coins.Value >= price then
print("A")
local stval = Instance.new("StringValue")
stval.Name = storagen
stval.Parent = player
player.Equippedst.Value = storagen
player.Storage.Value = storagef.amount.Value
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
return "Yes"
else
return "No"
end
elseif player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value ~= storagen then
local stval = Instance.new("StringValue")
stval.Name = storagen
stval.Parent = player
player.Equippedst.Value = storagen
player.Storage.Value = storagef.amount.Value
return "Equipped"
elseif player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value == storagen then
local stval = Instance.new("StringValue")
stval.Name = storagen
stval.Parent = player
player.Equippedst.Value = storagen
player.Storage.Value = storagef.amount.Value
return "Equipped"
end
else
if not player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value ~= storagen then
return "Not Owned" -- returns Buy and Not Owned
elseif player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value ~= storagen then
return "Owned" -- returns Owned
elseif player.Ownedst:FindFirstChild(storagen) and player.Equippedst.Value == storagen then
return "Equipped" -- returns Equipped
end
end
else
return "error"
end
end
return remotecon
--Server script
local remotecon = require(script.Parent.remotescript) -- wherever the module script is
local buytool = game.ReplicatedStorage.Remotes.buytool -- wherever the remote event is
buytool.OnServerInvoke = function(player, toolName, status)
local something = remotecon.buy(player,toolName, status)
return something
end
game.ReplicatedStorage.Remotes.strength.OnServerInvoke = function(plr , toolName)
plr.leaderstats.Strength.Changed:Connect(function(value)
math.clamp(plr.leaderstats.Strngth.Value , 0 , plr.Storage.Value)
end)
end
local buyst = game.ReplicatedStorage.Remotes.buyst -- wherever the remote event is
buyst.OnServerInvoke = function(player, storagen, status)
local something = remotecon.buyst(player,storagen, status)
return something
end
Thank you for reading this and please help me ASAP. Thank you in advance!