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.
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.
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.
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.
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)
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.
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.
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.