The game I'm working on uses some of the elements from the Simulator genre that's so popular right now. You gather 'salvage' in the game world and exchange it for 'silver' at the sell spot. The silver is stored in the leaderboard and is the game's primary currency. Here is my code for the sell spot:
local isTouched = false
script.Parent.Touched:Connect(function(hit)
local findhum = hit.Parent:FindFirstChild("Humanoid")
if findhum then
if not isTouched then
isTouched = true
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local silver = player.leaderstats.Silver
local packContainer = character:WaitForChild("Container") --Get player's bag
local packSpace = packContainer.Max.Value --Max amount the bag can hold
local packCur = packContainer.Value.Value --Current amount in the bag
silver.Value = silver.Value + packCur * 2 --Add silver to leaderboard
packContainer.Value.Value = 0 --Set bag to empty
wait(5)
isTouched = false
end
end
end)
You use the silver earned at one of the stores to buy new tools. Here's the code for the store that deals with the purchasing of a new tool.
local cam = workspace.CurrentCamera
local character = script.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local macheteSpawnEvent = game.ReplicatedStorage:WaitForChild("MacheteSpawnEvent")
local hammerSpawnEvent = game.ReplicatedStorage:WaitForChild("HammerSpawnEvent")
--Set camera to look at store
repeat wait()
cam.CameraType = Enum.CameraType.Scriptable
until cam.CameraType == Enum.CameraType.Scriptable
cam.CFrame = workspace.StartingStation.cameraPart1.CFrame
--Show Store Interface
player.PlayerGui.StoreGui.Frame.Visible = false
local screenStuff = player.PlayerGui.MacheteGui.Frame
screenStuff.Visible = true
local hammerScreenStuff = player.PlayerGui.HammerGui.Frame
--Disable normal gui
local StarterGui = game:GetService('StarterGui')
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
local mPresent = player.Backpack:FindFirstChild("Machete") --Check to see if player already has Machete
if mPresent == nil then
screenStuff.Buy.Active = true
else
screenStuff.Buy.Active = false
screenStuff.Buy.Text = "Purchased"
end
function buttonActive()
local silver = player.leaderstats.Silver --Get silver from leaderboard
local cost = game.Workspace.StartingStation.Machete.Value.Value -- Get cost of Machete
if cost < silver.Value then
player.leaderstats.Silver.Value = player.leaderstats.Silver.Value - cost --Subtract cost of machete from leaderboard
print("Silver after purchase.."..player.leaderstats.Silver.Value)
macheteSpawnEvent:FireServer(player) --Give player machete
screenStuff.Buy.Active = false
screenStuff.Buy.Text = "Purchased"
end
end
function closeActive()
screenStuff.Visible = false
--Return camera to player
game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"
--Enable normal gui
local StarterGui = game:GetService('StarterGui')
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
script:Destroy()
end
function rightActive()
screenStuff.Visible = false
hammerScreenStuff.Visible = true
cam.CFrame = workspace.StartingStation.cameraPart2.CFrame
end
function hammerLeftActive()
hammerScreenStuff.Visible = false
screenStuff.Visible = true
cam.CFrame = workspace.StartingStation.cameraPart1.CFrame
end
function hammerButtonActive()
local silver = game.Players.LocalPlayer.leaderstats.Silver
local cost = game.Workspace.StartingStation.Hammer.Value.Value
if cost <= silver.Value then
game.Players.LocalPlayer.leaderstats.Silver.Value = silver.Value - cost
hammerSpawnEvent:FireServer(player)
hammerScreenStuff.Buy.Active = false
hammerScreenStuff.Buy.Text = "Purchased"
end
end
function hammerCloseActive()
hammerScreenStuff.Visible = false
--Return camera to player
game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"
--Enable normal gui
local StarterGui = game:GetService('StarterGui')
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
script:Destroy()
end
function hammerRightActive()
end
screenStuff.Buy.Activated:connect(buttonActive)
screenStuff.CloseButton.Activated:connect(closeActive)
screenStuff.RightButton.Activated:Connect(rightActive)
hammerScreenStuff.LeftButton.Activated:Connect(hammerLeftActive)
hammerScreenStuff.Buy.Activated:connect(hammerButtonActive)
hammerScreenStuff.CloseButton.Activated:Connect(hammerCloseActive)
hammerScreenStuff.RightButton.Activated:Connect(hammerRightActive)
Once you buy the machete the leaderboard does update and show the proper amount of silver left. I can click into the player's leaderstats folder and clearly see that the Silver.Value has been changed accordingly. However... if I go collect another salvage and trade it for silver, the leaderboard jumps back to the original pre-purchase amount plus the new salvage. For example, I have 214 Silver. The machete costs 200 Silver. Leaderboard now shows 14 Silver. I collect 1 Salvage, which sells for 2 Silver. My new total should be 16, but instead the leaderboard now shows 216.
What have I done wrong?
