Still have questions? Join our Discord server and get real time help.
0

# Server Side Script with Datastore not running at all? [SOLVED BY DISCORD SERVER]

Edited 7 days ago

i am trying to use a data store but its not running not even a warning or error. i am new to lua and roblox stuff so please be easy on me.

function OnPlayerJoin()
local DataStoreService = game:GetService("DataStoreService")
local WoodStore = DataStoreService:GetDataStore("PlayerWood")
local StoneStore = DataStoreService:GetDataStore("PlayerStone")
local HasPlayedBefore = DataStoreService:GetDataStore("HasPlayedBefore")

local playerUserID = plr.UserId
local playerWood = 0
local PlayerStone = 0

local getSuccess, hasPlayed = pcall(function()
return HasPlayedBefore:GetAsync(playerUserID)
end)

if getSuccess then
print("Has PLayed Before")

else
print("Hasn't Played Before")
local setSuccess, errorMessage = pcall(function()
WoodStore:SetAsync(playerUserID, playerWood)
StoneStore:SetAsync(playerUserID, PlayerStone)
HasPlayedBefore:SetAsync(playerUserID, true)
end)
if not setSuccess then
warn(errorMessage)
end
end

end

wait(5)
players = game:GetService("Players")
for _,plr in ipairs(players:GetPlayers()) do
OnPlayerJoin(plr)
end


Thank you so much for taking the time to help me.

0
remove that for _, plr in ipairs loop, and just leave the players.PlayerAdded function CaioAlpaca 342 — 8d

1
Rinpix 360
8 days ago

Your code isn't working because you didn't put any parameters in the function's signature.

This:

function OnPlayerJoin()


Should look like this:

function OnPlayerJoin(plr)


Because a few lines after the declaration of the function, you used an undefined variable.

function OnPlayerJoin()
local DataStoreService = game:GetService("DataStoreService")
local WoodStore = DataStoreService:GetDataStore("PlayerWood")
local StoneStore = DataStoreService:GetDataStore("PlayerStone")
local HasPlayedBefore = DataStoreService:GetDataStore("HasPlayedBefore")

local playerUserID = plr.UserId -- By not specifying the "plr" parameter in the function's declaration it becomes undefined


The reason you're not getting any errors is because of this code:

print("Waiting For Server To Load")
wait(5)
players = game:GetService("Players")
for _,plr in ipairs(players:GetPlayers()) do
OnPlayerJoin(plr)
end


The first line shown here runs pretty much at the same time that the first player joins the game, so when it reaches the line where it waits 5 seconds, the event of the player joining the game has already fired and that player will not be accounted for, only ones that join after that one. Just remove the print statement, the wait statement, and the for loop. All you have to do is simply add "plr" inside of the parentheses in the function's declaration.

This is what the final code should look like:

function OnPlayerJoin(plr)
local DataStoreService = game:GetService("DataStoreService")
local WoodStore = DataStoreService:GetDataStore("PlayerWood")
local StoneStore = DataStoreService:GetDataStore("PlayerStone")
local HasPlayedBefore = DataStoreService:GetDataStore("HasPlayedBefore")

local playerUserID = plr.UserId
local playerWood = 0
local PlayerStone = 0

local getSuccess, hasPlayed = pcall(function()
return HasPlayedBefore:GetAsync(playerUserID)
end)

if getSuccess then
print("Has PLayed Before")

else
print("Hasn't Played Before")
local setSuccess, errorMessage = pcall(function()
WoodStore:SetAsync(playerUserID, playerWood)
StoneStore:SetAsync(playerUserID, PlayerStone)
HasPlayedBefore:SetAsync(playerUserID, true)
end)
if not setSuccess then
warn(errorMessage)
end
end
end