You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Alright, so I have a local script that when you click the button it fires the name of the button the player clicked to the server, then assigns that as their save slot key to DataStore.
What is the issue? Include screenshots / videos if possible!
Should I use :GetAsync() only on the .OnServerEvent or when the player joins?
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried figuring out ways to set the parameter as a variable to use throughout the script.
RegisterSlot.OnServerEvent:Connect(function(localPlayer, SlotName)
local SlotKey = localPlayer.Name.. " - "..SlotName
if localPlayer then
localPlayer:SetAttribute("Slot", SlotName)
localPlayer:SetAttribute("Key", SlotKey)
end
local success, errormessage = pcall(function()
local Data = SaveSlots:GetAsync(SlotKey)
SaveSlots:GetAsync(SlotKey)
end)
if success then
print("Successfully got slot!")
else
warn(errormessage)
end
end)
If you allow the player to select different slots, then it’s best to do in the event. Note that I would advice coding in a debounce so that player’s can’t spam the event.
Also, within your code, you are retrieving the data (twice) but aren’t doing anything with it. Make sure to store it outside of the pcall scope.
I had to do a lot of calculations in order to figure things out.
game.Players.PlayerAdded:Connect(function(Player) -- When the player enters the game.
local slotData
local attributeData
local SlotFolder = Instance.new("Folder")
SlotFolder.Name = "SlotFolder"
SlotFolder.Parent = Player
local SlotStage = Instance.new("IntValue")
SlotStage.Name = "SlotStage"
SlotStage.Value = 0
SlotStage.Parent = SlotFolder
local SlotAttributeKey = "-slotattribute"
local success, errormessage = pcall(function()
attributeData = SlotAttributes:GetAsync(SlotAttributeKey)
if attributeData then -- We have to check if the attributes are there otherwise we can't access the key!
print("Number of indexes: "..#attributeData)
Player:SetAttribute("Slot", attributeData[1])
Player:SetAttribute("Key", attributeData[2])
slotData = SaveSlots:GetAsync(Player:GetAttribute("Key"))
SetSlotData:FireClient(Player, slotData, attributeData)
end
end)
end)