I have created some leaderstats for the player, but when I add to the “Wheat” leaderstat, if it’s from clicking a button then I can use that wheat to sell, and it also saves using my datastore, but if it’s from harvesting wheat, I can’t sell it, and it doesn’t save from my datastore. This is shown in the video:
Here is a labeled picture of the relevant objects in my workspace:
All of the code listed above in the image:
SellModule
local module = {}
function module.sell(player)
local leaderstats = player:WaitForChild("leaderstats")
local wheat = leaderstats:WaitForChild("Wheat")
local money = leaderstats:WaitForChild("Money")
money.Value += wheat.Value * 5
wheat.Value = 0
end
return module
HarvestModule
local module = {}
function module.harvestCrop(part, plr)
part.Parent:Destroy()
local leaderstats = plr:WaitForChild("leaderstats")
local wheat = leaderstats:WaitForChild("Wheat")
wheat.Value += 1
end
return module
WheatModule
local module = {}
function module.wheat(player)
local leaderstats = player:WaitForChild("leaderstats")
local wheat = leaderstats:WaitForChild("Wheat")
wheat.Value += 1
end
return module
SellHandler
local ModuleScript = require(game.ReplicatedStorage:WaitForChild("SellModule"))
local PPS = game:GetService("ProximityPromptService")
PPS.PromptTriggered:Connect(function(prompt, player)
if prompt.Name == "SellPrompt" then
ModuleScript.sell(player)
end
end)
WheatHandler
local ModuleScript = require(game.ReplicatedStorage:WaitForChild("WheatModule"))
local PPS = game:GetService("ProximityPromptService")
PPS.PromptTriggered:Connect(function(prompt, player)
if prompt.Name == "WheatPrompt" then
ModuleScript.wheat(player)
end
end)
UserInputScript
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local camera = workspace.CurrentCamera
local item = nil
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ModuleScript = require(game.ReplicatedStorage:WaitForChild("PlantModule"))
local ModuleScript2 = require(game.ReplicatedStorage:WaitForChild("HarvestModule"))
local function CreateRayCast()
local mousePosition = uis:GetMouseLocation()
local rayCast = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local rcResult = workspace:Raycast(rayCast.Origin, rayCast.Direction * 100)
return rcResult
end
uis.InputBegan:Connect(function(input, busy)
if input.UserInputType == Enum.UserInputType.MouseButton1 and not busy then
if item:IsA("BasePart") and item.Parent.Name == "Plot" then
local hasCrop = #item:GetChildren() > 0
local part = item
if hasCrop == false then
ModuleScript.plantCrop(part)
end
end
if item:IsA("BasePart") and item.Parent.Name == "Wheat3" then
local part = item
local plr = player
ModuleScript2.harvestCrop(part, plr)
end
end
end)
rs.RenderStepped:Connect(function()
local result = CreateRayCast()
if result and result.Instance then
item = result.Instance
end
end)