Assistance with RemoteEvent

Essentially, what I need is when the Wheat Purchase button is pressed, Wheat Seed bag is given.
When the Rice Purchase Button is pressed, Rice Seed bag is given.

When it Fires the event, the event will run both functions, which will then give 2 tools, rather than 1 individualized tool.

I have tried Creating 2 different Variable function names to make both singular, and fire each individual Event when the buttons are pressed accordingly.

Still fires both, and 2 Seed bags go to the players backpack.

I need to fix the Remote Event Script, to where when an individual button is pressed, that it will Fire the Event, and fire a copy of the tool to the backpack.

Currently, it will give 2 tools when Either button is pressed.

  • Seedbag Script

--  Variables


local wheat = script.Parent.WheatButton
local rice = script.Parent.RiceButton
local sugarcane = script.Parent.SugarcaneButton

local seedbags = game.Lighting.Seedbags


local player = game.Players.LocalPlayer

local N = 1

-- Value Variables

local wheatAmount = wheat.AmountValue
local riceAmount = rice.AmountValue
local sugarcaneAmount = sugarcane.AmountValue


--  Functions




--  Script Logic


wheat.MouseButton1Click:Connect(function()
		
	if wheatAmount.Value >= 1 then
		wheatAmount.Value -= 1
		
	game.ReplicatedStorage.RemoteEvent:FireServer(A1)
		
	end	
end)

rice.MouseButton1Click:Connect(function()

	if riceAmount.Value >= 1 then
		riceAmount.Value -= 1

	game.ReplicatedStorage.RemoteEvent:FireServer(players2)

	end	
end)

while wait() do
	
	task.wait(.5)
	
	wheat.AmountLabel.Text = wheatAmount.Value
	rice.AmountLabel.Text = riceAmount.Value
	sugarcane.AmountLabel.Text = sugarcaneAmount.Value

end
  • Remote Event Script
game.Players.PlayerAdded:Connect(function(player)

		
	
--  Variables

local wheat = game.StarterGui.SeedStorageGui.PrimaryFrame.WheatButton

local seedbags = game.Lighting.Seedbags

local wheatAmount = wheat.AmountValue


--  Functions




--  Script Logic
		
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(A1)

	local wheatBag = seedbags.Wheat:Clone()	
		wheatBag.Parent = player.Backpack
		print("Worked!") 
			
	end)
	
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(players2)

		local riceBag = seedbags.Rice:Clone()	
		riceBag.Parent = player.Backpack
		print("Worked!") 

	end)	
	
end)

You’ve connected two callbacks to the same event ReplicatedStorage.RemoteEvent. When you fire that event from the client, both callbacks will run. You can do two things:

  • Use two separate remote events: one for wheat and one for rice. Then on the client you fire the appropriate one.
  • Use one remote event but have an argument that specifies whether it’s wheat or rice. You should only connect one callback and then check that argument to see whether to add wheat or rice.

Also as a side note:

if wheatAmount.Value >= 1 then
		wheatAmount.Value -= 1

Checks like these should really be handled server-side so as to avoid exploiters from getting infinite wheat/rice bags

1 Like

Thank you greatly for your reply. I appreciate this so much!

1 Like

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