How to use RemoteEvents efficiently?

I’m trying to be able to buy 2 different swords with a single RemoteEvent. How do I do this?
(It’s not a server-side issue, client-side)

The issue is in checkRocket().
When ClassicalSword is bought, it also returns true for VenomSword; I’m attempting to make it NOT return true if ClassicalSword is bought and VenomSword isn’t. Vice versa.

local Player = game.Players.LocalPlayer
local buyEvent = game.ReplicatedStorage.BuyEvent
local hasWeapon = false

local function checkRocket()
	local found = false
	for i, v in pairs (Player.Backpack:GetChildren()) do
		if v.Name == "ClassicSword" then
			found = true
		elseif v.Name == "VenomSword" and not v.Name == "ClassicSword" then
			found = true
		end
	end
	
	for i, v in pairs (Player.Character:GetChildren()) do
		if v.Name == "ClassicSword" then
			found = true
		elseif v.Name == "VenomSword" and not v.Name == "ClassicSword" then
			found = true
		end
	end
	
	if found == true then
		return true
	else
		return false
	end
end

script.Parent.Frame.Sword1.MouseButton1Click:Connect(function()
	if Player.leaderstats and Player.leaderstats.Points.Value >= cost then	
		hasWeapon = checkRocket()
		if not hasWeapon then
			buyEvent:FireServer('ClassicSword')
		end
	end
end)

script.Parent.Frame.Sword2.MouseButton1Click:Connect(function()
	if Player.leaderstats and Player.leaderstats.Points.Value >= cost2 then
		hasWeapon = checkRocket()
		if not hasWeapon then
			buyEvent:FireServer('VenomSword')
		end
	end
end)

Your system is flawed because you let the client handle your data for the server. Anyone with basic knowledge in exploiting can give themselves a sword they want.

1 Like

Interesting. I am not experienced in anti exploiting, so maybe can you give me an idea of how to rework the system?

Nevermind- I forgot I am able to handle the cost on serverside

Your search loops seems not needed. If you want to know if something under a name is inside of an instance, try using FindFirstChild.

local name = 'ClassicSword'
local hasWeapon = Player.Backpack:FindFirstChild(name) ~= nil
-- notice `hasWeapon` is local because we don't want side effects

if not hasWeapon then
    buyEvent:FireServer(name)
end

Of course, like nooneisback said, you should be doing both the weapon check and price check on the server, and only sending the requested name over the remote.

Also, your if Player.leaderstats check is not needed; if leaderstats doesn’t exist, this would just error anyways.

1 Like

Holy guacamole! Thank you bro!