Hello, I have a issue where a remoteEvent fires then the remoteFunction fires. It works the first time but then once it’s executed the 2nd time does not fire.
Thoughts of what might could be wrong would help!
remotes.new.OnServerEvent:Connect(function(plr,slot1,slot2)
local weapons = {}
remotes.weapons.OnServerInvoke = function()
weapons[1] = slot1
weapons[2] = slot2
for i,v in pairs(weapons) do
-- for later
end
return weapons
end
end)
It’s very unlikely that you want to be setting the callback function of remotes.weapons everytime the remotes.new RemoteEvent is triggered. Could you provide a bit more detail on how/when you are using both the RemoteEvent and the RemoteFunction and also explain what you expect both of them to do?
Sure! The purpose of firing a remoteFunction inside a remoteEvent is because I have a module that fires the remoteEvent when the player selects a loadout then that remote creates a weapon table storing the weapons name based on string Values from the server then the remote function returns that table and in a separate script that remoteFunction is used to loop through it and connect my FPS framework which takes care of all of it from there (which isn’t the problem).
Ok so your issue might be related to the the remotes.weapons callback being shared for all clients. Every time a player triggers the remotes.new RemoteEvent remotes.weapons will be updated to a new function referencing different arguments. To get around this you might want to pull the weapons table out of the RemoteEvent bound function to turn it into a global variable. You could then also pull the RemoteFunction out of the RemoteEvent bound function and just change the way you access the data slightly. Here’s how I would set it up:
GLOBAL_WEAPONS = {} --initially empty, calls to remotes.new will fill it for the calling player
remotes.new.OnServerEvent:connect(function(plr, slot1, slot2)
GLOBAL_WEAPONS[plr] = {
Slot1 = slot1,
Slot2 = slot2
}
end)
remotes.weapons.OnServerInvoke = function(plr)
local weapons = GLOBAL_WEAPONS[plr]
if not weapons then
--if the calling player has not yet called remotes.new, then the entry in the GLOBAL_WEAPONS table will be nil,
--you might want to then fill the table at that key with some sort of default table or throw an error
end
for i, v in pairs(weapons) do
--for later
end
return weapons
end
After a few hours of extending upon your response I got it to work! Thanks! What I did was I would have a value that gets changed every time that the weapons table was filled out and then it would go to the client and then the client would listen for a .Changed event to see if the value was changed and if it was it would check if its true by an if statement then call a equip function on the client/