Event not firing

I have this bug where the event doesnt fire for the wheat (which is the crop you harvest with your tool) and you need to re equip the tool so you can harvest the new spawned wheat, I have a video documenting the bug. Thank you for helping!

local HarvestEvent = game.ReplicatedStorage.HarvestEvent
local tool = script.Parent

tool.Equipped:Connect(function()
	HarvestEvent:FireServer()
end)
while true do
	for _, v in ipairs(crop_spawn) do
		local spawnPosition = v.Position
		if not hasCropAtPosition(spawnPosition) then
			-- Introduce some randomness to the spawn position
			local offsetX = math.random(0, 1) -- Adjust the range as needed
			local offsetY = math.random(0, 3) -- Adjust the range as needed
			local offsetZ = math.random(-1, 1) -- Adjust the range as needed

			local newCrop = cropPrefab:Clone()
			newCrop.Name = "Crop"
			newCrop.Parent = workspace
			newCrop.Position = spawnPosition + Vector3.new(offsetX, offsetY, offsetZ)
			local newPrompt = proxPrompt:Clone()
			newPrompt.Parent = newCrop
			HarvestEvent.OnServerEvent:Connect(function()
				newPrompt.Triggered:Connect(function(player)
					newPrompt:Destroy()
					newCrop:Destroy()
					
				end)
			end)
			break -- Exit the loop once a crop is spawned
		end
	end
	wait(10) -- Wait for 10 seconds before attempting to spawn crops again
end

Based on the video, if a crop spawns while the player is already equipping the tool, HarvestEvent.OnServerEvent will never trigger and the callback for the prompt event will never run. To be able to harvest the crop again, you’d have to re-equip the tool like you did in the video so the HarvestEvent remote event will fire from the client and all the existing crops can add a handler to the prompt.

3 Likes

So what are you suggesting me to do?

1 Like

recode it
your technique isnt really good for this.

1 Like

what do you mean recode it bruh I aint spending 5 hours of trying to fix bugs for allat

1 Like

I guess you could spam fire it. Or fire it everytime crop spawns or what not. I’m clueless lol.

im brainstorming i guess you could fire a client event on the server when the crop is harvested which in turn fires the server event on the client

1 Like

My immediate suggestion would be to just keep track of whether or not the player has the tool equipped on the server and use that in your crop spawning code.

  • When a player equips a tool, keep track of that in some accessible variable. In addition, create and initialize the prompts for any currently existing crops.
  • When a player removes the tool, destroy the prompts on any existing crops (iirc, destroying the instance automatically disconnects any signals) and update the holding state.
  • When a crop spawns, it checks whether the player has the tool equipped via the variable.
    • If they have the tool equipped, create and initialize the prompt
    • If they don’t have the tool equipped, don’t do anything.
1 Like

My friend to say wrap it in pcall but it returned success and the event fires once you equip the tool, is that the problem? or is their more to it.

Wrap what in pcall? From what I can tell, wrapping your loop in pcall isn’t going to change the behavior at all.

pcall is used to run code that might throw errors. It lets you catch those errors and handle them accordingly instead of the script just dying immediately once an error occurs.

1 Like

firing the server once the tool is equiped

local tool = script.Parent

tool.Equipped:Connect(function()
	while true do
		local success, returnval = pcall(function()
			HarvestEvent:FireServer()
		end)
		if success then
			print("Code Run Successfully!")
			break
		else
			warn(string.format("Error when executing code!\nError: %s", returnval))
			-- Code to handle errors here
			wait(1) -- Wait before retrying
		end
	end
end) ```

This is going to do the exact same thing you originally had but now with more confusion about what the code is actually doing. As far as I know, FireEvent can never throw an error so there’s no point in wrapping in pcall.

That means success is always going to be true and the while loop will break after the first iteration. So might as well not have the while loop at all.

so i should just destroy the remote event and check if the player has a tool equipped on the server side instead?

Im a little confused on whats going on here or rather why

newPrompt.Triggered:Connect(function(player)
	--check if player has a valid HarvestingTool by accessing .Character if so destroy crop
	newCrop:Destroy()
end)

no idea what or why .equipped needs to fireserver

Possibly. I haven’t really worked with tools so I’m not real familiar with how they work. If tool.Equipped can be triggered on the server, you can get rid of the remote event and have it all be server-side. If tool.Equipped can’t be triggered from the server, you’d still need the remote event so the client can tell the server that they are equipping the tool.

the problem still occurs, this is the weirdest bug ive dealt with, why does it have to be so confusing man.

never mind! it worked, i disconnected the remote and it worked, im not kidding you i spent 9 hours in all fixing this bug today.

I think what you should do instead is just check if the character’s child has the tool in it. And then fire the stuff I guess. Sorta like this:

newprompt.Triggered:Connect(function(player)
if player.Character and player.Character:FindFirstChildOfClass("Tool") then
HarvestEvent:FireServer()
end
end)

Also it could be because you’re doing this on the client for some reason, I get that the client reacts faster but c’mon. A little delay wouldn’t hurt would it? Plus using a remote event unneccessarily like this would use more resources rather than just doing it on server and there is always the risk of it being exploitable.

I have a question, i got the tool from the starter pack instead of backpack, is that bad?

Although it worked like I said I think you should just do it fully server-sided and just check when the prox prompt is triggered that the player’s character has the tool in it. Although the client reacts faster I don’t see the need. Server is safer anyway.

Yeah that worked, i just did starter pack for testing because its easier. I prob will do backpack later when the peasant spawns in