Remote event not activating properly

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.

i think the issue is that you dont have a debounce when activating the tool.

1 Like

A debounce? It still feels weird that it creates the exact number of things i have on my folder, but i’ll try it out

Can you parent the crop clone in the server script after you set the position?

1 Like

You mean like parenting the crop clones inside the script that creates it?

I mean when you parent it here

Can you try parenting the crop clone after this part of the code instead of parenting to the workspace instantly.

1 Like

Yeah just tried it, same thing, however i discovered that even if i comment the whole code and just keep the script like this:

remote.OnServerEvent:Connect(function(player, toolName, target)
print(“Remote event received:”, toolName, “on”, target.Name)

It still prints a lot of times:


So there’s something on my tool

Ok i just realized that the remote event is gonna fire for every single one of my instances with dirt. Makes sense. I’m probably gonna have to rewrite everything since the actual logic is broken, as i was using the same remote event for all dirt blocks. :frowning:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.