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!
i want players to be given one random tool they own from a datastore when they join the game
- What is the issue? Include screenshots / videos if possible!
i dont know how to give them one random tool that they own from there datastore
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i been looking on the developer hub and on youtube but did not find any way to fix it
thats the code i got so far
local datastoreService = game:GetService("DataStoreService")
local myDataStore = datastoreService:GetDataStore("SAVED_TOOLS")
local NightTools = game:GetService("ServerStorage"):WaitForChild("NightTools")
function giveRandomTool(player)
local success, data = pcall(function() return myDataStore:GetAsync(player.UserId) end)
if success then
-- Player has existing data
if data.Tools then
-- Player already has tools, do nothing
return
end
end
-- Get a random tool from ServerStorage
local allTools = NightTools:GetChildren()
local randomIndex = math.random(1, #allTools)
local randomTool = allTools[randomIndex]:Clone()
-- Equip the tool to the player
player.Character:WaitForChild("Humanoid").EquipTool(randomTool)
-- Update the datastore (if necessary)
if success then
data.Tools = {randomTool.Name} -- Store the given tool name
pcall(function() myDataStore:UpdateAsync(player.UserId, data) end)
else
local data = {Tools = {randomTool.Name}}
pcall(function() myDataStore:SetAsync(player.UserId, data) end)
end
end
-- Example usage:
game.Players.PlayerAdded:Connect(giveRandomTool)