Could anyone tell me what am i doing wrong here? im making a harvest and plant system similar to grow a garden. im curious on why its failing
folders on server storage: Items - seeds
ItemsPlanted - The model for crops when planted
finished - When harvested you get the tools from it.
theres 2 RemoteEvents on replicatedstorage. they are PlantSeed and Harvest item. Here is my mode
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local plantEvent = ReplicatedStorage:WaitForChild("PlantSeed")
local harvestEvent = ReplicatedStorage:WaitForChild("HarvestPlant")
local Items = ServerStorage:WaitForChild("Items")
local Planted = ServerStorage:WaitForChild("ItemsPlanted")
local Finished = ServerStorage:WaitForChild("Finished")
local growInterval = 1 -- debug
local maxY = 1
local startY = -8
local playerDataStore = DataStoreService:GetDataStore("PlantData")
local growingPlants = {}
local function savePlant(player, soilName, plantName, yPos)
local key = player.UserId.."_"..soilName
local success, err = pcall(function()
playerDataStore:SetAsync(key, {Plant = plantName, Y = yPos})
end)
if not success then
warn("Failed to save plant:", err)
end
end
Players.PlayerAdded:Connect(function(player)
local gardenFolder = workspace:FindFirstChild(player.Name.."garden")
if not gardenFolder then return end
for _, soil in ipairs(gardenFolder.Soil:GetChildren()) do
local key = player.UserId.."_"..soil.Name
local success, data = pcall(function()
return playerDataStore:GetAsync(key)
end)
if success and data then
local plantModel = Planted:FindFirstChild(data.Plant)
if plantModel then
local clone = plantModel:Clone()
clone.Parent = soil
clone.Position = Vector3.new(soil.Position.X, data.Y, soil.Position.Z)
if data.Y < maxY then
growingPlants[clone] = {Player = player, Soil = soil, Y = data.Y, PlantName = data.Plant}
else
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Harvest"
prompt.ObjectText = data.Plant
prompt.Parent = clone
prompt.Triggered:Connect(function()
harvestEvent:FireServer(player, soil, clone)
end)
end
end
end
end
end)
plantEvent.OnServerEvent:Connect(function(player, soil, seedName)
if soil.Ownership.Value ~= "" then return end -- occupied
if not player.Character then return end
local tool = player.Character:FindFirstChildOfClass("Tool")
if not tool or tool.Name ~= seedName then return end
local plantModel = Planted:FindFirstChild(seedName)
if not plantModel then return end
tool:Destroy()
soil.Ownership.Value = player.Name
local clone = plantModel:Clone()
clone.Parent = soil
clone.Position = Vector3.new(soil.Position.X, startY, soil.Position.Z)
growingPlants[clone] = {Player = player, Soil = soil, Y = startY, PlantName = seedName}
savePlant(player, soil.Name, seedName, startY)
end)
local harvestEvent = ReplicatedStorage:WaitForChild("HarvestPlant")
local Finished = ServerStorage:WaitForChild("Finished")
harvestEvent.OnServerEvent:Connect(function(player, soil, plant)
if not (plant and plant.Parent == soil) then return end
local finishedItem = Finished:FindFirstChild(plant.Name)
if finishedItem then
local clone = finishedItem:Clone()
clone.Parent = player.Backpack
end
local doNotDelete = plant:FindFirstChild("donotdeleteafterharvest")
if not doNotDelete or (doNotDelete:IsA("BoolValue") and doNotDelete.Value == false) then
plant:Destroy()
soil.Ownership.Value = ""
end
end)
while true do
for plant, info in pairs(growingPlants) do
if plant and plant.Parent then
if info.Y < maxY then
info.Y += 1
plant.Position = Vector3.new(info.Soil.Position.X, info.Y, info.Soil.Position.Z)
savePlant(info.Player, info.Soil.Name, info.PlantName, info.Y)
if info.Y >= maxY then
-- Add harvest prompt
local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Harvest"
prompt.ObjectText = info.PlantName
prompt.Parent = plant
prompt.Triggered:Connect(function()
harvestEvent:FireServer(info.Player, info.Soil, plant)
end)
growingPlants[plant] = nil
end
end
end
end
wait(growInterval)
end
If you know where i went wrong, please let me know!