Help with a Remote Event not working

so I have a simple character selection script here, for when you press a button (from top bar plus) it will fire a Remote Event (CharacterSelected) that will go to this script in StarterCharacterScripts here:

local RepStore = game:GetService("ReplicatedStorage")
local GameCharacter = RepStore.Characters.PlaceholderCharacter
local AssetsFolder = GameCharacter.Assets
local RemotesFolder = GameCharacter.Remotes

local Players = game:GetService("Players")
local myCharacter = script.Parent
local myPlayer = Players:GetPlayerFromCharacter(myCharacter)

RemotesFolder.CharacterSelected.OnServerEvent:Connect(function(remotePlayer)
	if remotePlayer ~= myPlayer then
		return end
		RemotesFolder.CharacterSelected:FireClient()
end)

which it will then fire again, only its “FireClient()” now because its in a server script. I would fire client directly when you press the button other then this weird way, but the script is local so i have to do this. There will be a script in StarterGui that will recieve the FireClient() but whenever i run this, the script gets this weird error:
Argument 1 missing or nil
I have no idea what this means, if anyone could please help or revamp my scripts entirely, that would be huge.

Also the way I store characters into the user sucks. Literally just a string value that you get when you spawn so changing it wouldn’t be permanent. If people could help make one that saves even when you leave or reset it would be awesome! thanks.

You provided the wrong script, you literally told us the error occurs once it reaches the client, so you should’ve provided the client script.

Also if the only reason you have a remote is to from the client, redirect back to the client, use a bindable event instead.

Also you could store the string value as an attribute on the player and whenever the player’s character is added load your character that is referenced in the stringvalue. This way when you reset it doesn’t go away. As for saving, use DataStoreService

i haven’t made the script in starter gui yet due to this error.

You need to pass which player you’re firing to. That’s why it’s screaming the first argument is missing is because the player you’re firing to is nil

1 Like

How would i set the attribute and DataStore up? I’m not very experienced so I need a walk-through.

Hey, jiily3678.

The issue is that you aren’t passing the player argument in the FireClient method. Do remember that FireClient requires a player to be used as the first argument. If you want to send the event for all players then use FireAllClients.

--you add attributes just using SetAttribute
YourPlayer:SetAttribute("Character", "name of char")
--they work on any instance

As for the datastore you want to get a datastore for the player, when the player joins you load the value from datastore, when they leave or the game closes down you save it, and maybe you auto save every once in a while.

--simplified example that shouldn't actually be used
local players = game:GetService("Players")
local datastores = game:GetService("DataStoreService")

local yourstore = datastores:GetDataStore("yourname")

local function save(plr: Player)
      yourstore:SetAsync("Character"..plr.UserId, plr:GetAttribute("Character"))
end

players.PlayerAdded:Connect(function(plr)
      local success, data = pcall(function()
            return yourstore:GetAsync("Character"..plr.UserId)
      end)

      if not success then
          plr:Kick("failed to load data") 
      else
          plr:SetAttribute("Character", data)
      end
end)

players.PlayerRemoving:Connect(save)
game:BindToClose(function()
   for _, p in ipairs(players:GetPlayers()) do
       save(p)
  end
end)

This is a simplified example that does the absolute minimum, normally you 'd add retrying mechanisms, cacheing, session locking, and you might name or sort things differently, but that is outside of the scope of a single devforum comment, I suggest you read the documentation of datastoreservice:

DataStoreService

Also I’m typing this on my phone and I just wrote this off the top of my head so if I got some of the api method names wrong then sorry, but the structure should be the same anyway.

Also what @sonic_848 said is the answer so you should mark it as the solution. I thought it said FireAllClientsđź’€

Edit: not me, I didn’t solve anything.

1 Like

but what you did say (make it a bindable event) worked perfectly.
also where do i place the two scripts for the attributes and datastore?

Datastoreservice can only be accessed on the server, it doesn’t matter where you put it as long as it’s on the server. I suggest ServerScriptService though.

Don’t copy the example above, as it is missing a lot of key features, it is just there to show you the bare minimum for it to work. But you should write your own.

Got it. I think I’ll just post another topic on the dev forum about this because like i said earlier, im not too experienced.

If I remember correctly, apart from the normal datastoreservice documentation, roblox actually has a guide for Data Stores.

Here I found it:
Data Stores

Edit: nvm I couldn’t find the model, did they remove it? Anyway it is still a guide on data stores

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