Simple Join Function Won't Run?

Hey there!

I am absolutely stumped on as to why this won’t work, and it is SO simple!

The code

function onPlayerJoin(player)
	print("1")
end
game.Players.PlayerAdded:Connect(onPlayerJoin)

The little annoying thing refuses to print “1” :rofl: :face_with_raised_eyebrow:
I believe I am doing everything right, but who knows you all might see the issue and then I can go bang my head against a wall in unbelief…

2 Likes

I just copied your code and works perfectly fine just make sure it is in a script an not a local script and i guess it should work

1 Like

Where did you put this script? Is it a server script or a local script?

1 Like

It is a server script, it works for you maybe it is something outside the function,

local nameData = game:GetService("DataStoreService"):GetDataStore("NameData")
local itemKey = "CurrentItems"
local name = nil

local callTasks = game.ServerStorage.CallForTasks
local callPrems = game.ServerStorage.CallForPrems
local UpdateDataName = game.ServerStorage.UpdateDataName
local callData = game.ReplicatedStorage.CallData

local bTask1 = nil
local bTask2 = nil
local bTask3 = nil
local pTask1 = nil
local pTask2 = nil
local pTask3 = nil

local bComplete1 = nil
local bComplete2 = nil
local bComplete3 = nil
local pComplete1 = nil
local pComplete2 = nil
local pComplete3 = nil

TimeWait = false

local dataName = UpdateDataName:Invoke()
print(dataName)
function onNameNil()
	if nameData:GetAsync(itemKey) == nil then
		print("Changing Item Data")
		local tasksTable = callTasks:Invoke()
		local premiumTasksTable = callPrems:Invoke()
		nameData:SetAsync(itemKey, {tasksTable[1], tasksTable[2], tasksTable[3], premiumTasksTable[1], premiumTasksTable[2], premiumTasksTable[3]})
	end
end	
onNameNil()
local data = game:GetService("DataStoreService"):GetDataStore(dataName)

print(data)
function onPlayerJoin(player)
	print("1")
	--[[
	local timeKey = "user_" .. player.userId .. "_time"
	local tasksKey = "user_" .. player.userId .. "_Tasks"
	local completeKey = "user_" .. player.userId .. "_Complete"
	if data:GetAsync(timeKey) == nil then
		data:SetAsync(timeKey, true)
		data:SetAsync(tasksKey, {0,0,0,0,0,0})
		data:SetAsync(completeKey, {false,false,false,false,false,false})
	end
	print("2")
	local dataStats = nil
	local dataStats2 = nil
	local success, err = pcall(function()
		dataStats = data:GetAsync(tasksKey)
		dataStats2 = data:GetAsync(completeKey)
	end)
	if success then
		print("Successfully Got " .. player.Name .. "'s Data!")
	else
		repeat 
			warn("Retrying, count:", "\nError:", err)
			wait(7)

			success, err = pcall(function()
				dataStats = data:GetAsync(tasksKey)
				dataStats2 = data:GetAsync(completeKey)
			end)
		until success
	end
	print("3")
	if dataStats[1] == nil then
		data:SetAsync(tasksKey, {0,0,0,0,0,0})
		data:SetAsync(completeKey, {false,false,false,false,false,false})
	end
	 bTask1 = dataStats[1]
	 bTask2 = dataStats[2]
	 bTask3 = dataStats[3]
	 pTask1 = dataStats[4]
	 pTask2 = dataStats[5]
	 pTask3 = dataStats[6]
	
	 bComplete1 = dataStats2[1]
	 bComplete2 = dataStats2[2]
	 bComplete3 = dataStats2[3]
	 pComplete1 = dataStats2[4]
	 pComplete2 = dataStats2[5]
	 pComplete3 = dataStats2[6]
	print("4")
	TimeWait = true
	local function CallData(player, number)
		print("Recieved")
		repeat
			wait(1)
			print("Waiting")
		until TimeWait == true
		print(bTask1,bTask2,bTask3,pTask1,pTask2,pTask3,bComplete1,bComplete2,bComplete3,pComplete1,pComplete2,pComplete3)
		if number == 1 then
			return bTask1, bComplete1
		elseif number == 2 then
			return bTask2, bComplete2
		elseif number == 3 then
			return bTask3, bComplete3
		elseif number == 4 then
			return pTask1, pComplete1
		elseif number == 5 then
			return pTask2, pComplete2
		elseif number == 6 then
			return pTask3, pComplete3
		else
			return "ERROR"
		end
	end
	callData.OnServerInvoke = CallData
	
	local folder = Instance.new("Folder")
	folder.Name = "TaskFolder"
	folder.Parent = player
	--]]
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")

would anything before that create an issue?
(that is the whole script before the function)

Try commenting out this block.

local dataName = UpdateDataName:Invoke()
print(dataName)
function onNameNil()
	if nameData:GetAsync(itemKey) == nil then
		print("Changing Item Data")
		local tasksTable = callTasks:Invoke()
		local premiumTasksTable = callPrems:Invoke()
		nameData:SetAsync(itemKey, {tasksTable[1], tasksTable[2], tasksTable[3], premiumTasksTable[1], premiumTasksTable[2], premiumTasksTable[3]})
	end
end	
onNameNil()

If print(data) prints the data meaning that there are no errors in the code, then the event connection is created after the player joins and that is the reason why this is not working.

1 Like

ok what should I do now, @HammyLammyYT thing worked, but I need that function to go before the playeradded function, otherwise it won’t work?

Let me explain into further detail, @MRKYLO20

  • 1st: I do not know if :Invoke() exists as a function. I don’t know exactly what CallForTasks, CallForPrems, UpdateDataName and CallData is, so I can’t tell you more really. Could you tell me what type of objects they are?
  • 2nd: If it’s a RemoteFunction, then there is a possibility that the server is waiting for a response, that is never coming. If it never comes, the script doesn’t resume and would just stop right then and there.

the first three are bindable functions, and the last one is a remote function yes

Are you sure any script is assigned to the invokes? There has to be an .OnInvoke function somewhere else that returns something, otherwise, like I said, the script won’t continue execution. Same with the remote function.

oh there definitely is scripts invoking them, what should I do to make it not an issue?

Are you doing this in studio, for some reason, testing in studio loads the player before the server. You might need to call a for loop with all the current players with the same function before the connection.

1 Like

Like this?

for i, player in pairs(game.Players:GetPlayers()) do
	print("playerFound")
end
game.Players.PlayerAdded:Connect(onPlayerJoin)

I tried that thing and it didn’t work…

Is the script in Workspace or ServerScriptService?

Serverscriptservice

characters that amount to 30

Try this:

game.Players.PlayerAdded:Connect(function(player)
	print("1")
end)
1 Like

AAAHH!

That’s annoying, no that doesn’t work either

There is something waiting before this is firing. This means that the player is joining, and then the PlayerAdded function is connecting so it never fires. Try moving it to a clean new script.

edit: The invoke is the problem

The only thing I could think of is that the Script is disabled…

I have no idea why that script wouldn’t work… the example on the PlayerAdded page is pretty much the same as that script!

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded