I’ve been trying to do a farming game of some sort and i want the player to be able to create it’s own path of dirt and grow crops on it. That’s it.
However, it seems like the remoteEvent that was supposed to activate the crop tool and put it right above the dirt block is not working properly.
Whenever i click with any of my crop tools, it creates the same number of crops as i have of dirt blocks, which is not supposed to happen. And yeah, it’s a pretty amateur code, maybe there’s something inside of my tools that isn’t working properly but i searched all through the forum and haven’t found anything, so if someone could figure why this keeps happening i’d be so glad
Code inside all of my crop tools(Corn, Wheat, Etc)
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local remote = game:GetService("ReplicatedStorage").GetEvent
tool.Activated:Connect(function()
local target = mouse.Target
if target.Name == "Wheat" or target.Name == "Corn" or target.Name == "Carrot" then
remote:FireServer(tool.Name, target)
return true
end
end)
Code used in the dirt blocks that the crops are supposed to be above of:
local remote = game:GetService("ReplicatedStorage").ClickEvent
local mod = require(game:GetService("ReplicatedStorage").CropsFolder.Crops)
local isWater = script.Parent.ProximityPrompt:WaitForChild("Regado")
local crops = game:GetService("ServerStorage").Crops
local cropObjects = {
Wheat = crops.Wheat,
Carrot = crops.Carrot,
Corn = crops.Corn
}
local dirtStates = {}
local function growCrop(cropClone, cropName, target)
if isWater.Value then
local cropData = mod[cropName]
if cropData then
task.delay(cropData.cropTime, function()
dirtStates[target] = nil
print("Crop grown:", cropName, "on", target.Name)
-- Optionally, destroy the grown crop if needed:
-- cropClone:Destroy()
end)
end
else
print("Not watered.")
return false
end
end
remote.OnServerEvent:Connect(function(player, toolName, target)
print("Remote event received:", toolName, "on", target.Name)
-- Ensure dirtStates is handling the target correctly
if not dirtStates[target] and cropObjects[toolName] then
print("Planting:", toolName, "on", target.Name)
dirtStates[target] = true -- Mark the dirt block as occupied
local cropClone = cropObjects[toolName]:Clone()
cropClone.Parent = workspace
wait(0.5)
-- Ensure crop is placed at the target's position
if toolName == "Wheat" then
cropClone.Position = target.Position + Vector3.new(0, 5, 0)
elseif toolName == "Corn" then
cropClone:PivotTo(target.CFrame + Vector3.new(0, 3, 0))
elseif toolName == "Carrot" then
cropClone:PivotTo(target.CFrame + Vector3.new(0, 1, 0))
end
-- Destroy the tool in the player's character
if player.Character:FindFirstChild(toolName) then
player.Character:FindFirstChild(toolName):Destroy()
end
-- Wait until the dirt block is watered
while not isWater.Value and cropClone.Parent == workspace do
task.wait(1)
end
growCrop(cropClone, toolName, target)
else
print("Dirt block is already occupied or invalid crop.")
end
end)
Local script inside of the tool that creates the dirt blocks:
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local remote = script.Parent.Create
local dirt = game:GetService("ReplicatedStorage").Dirt
local dirtCount = 0
tool.Activated:Connect(function()
local target = mouse.Target
local hit = mouse.Hit.Position
if target.Name == "Grama" then
remote:FireServer(hit)
end
end)
Script inside of the tool that actually creates the blocks:
local tool = script.Parent
local remote = script.Parent.Create
local dirt = game:GetService("ReplicatedStorage").Dirt
local dirtCount = 0
remote.OnServerEvent:Connect(function(player, mousePos)
dirtCount = dirtCount + 1
local newDirt = dirt:Clone()
newDirt.Parent = workspace.DirtFolder
newDirt.Position = mousePos
newDirt.Name = newDirt.Name..tostring(dirtCount)
end)
I tried giving all that’s necessary, if there’s something i also need to provide then just tell me cuz i’m kinda dumb.