Admin level script wont work

When I join, I’m supposed to get the value 4 since I’m the owner, however, it’s error.

Script:

local saveData = game:GetService("DataStoreService"):GetDataStore("ezdsf,dkfdsdjsdojdskhdskdnsk")

function setAdminLevel(userid,level)
	local name = game:GetService("Players"):GetPlayerByUserId(userid)
	name.Administrative.AdminLevel.Value = level
	saveData:SetAsync(userid,level)
end

game.Players.PlayerAdded:Connect(function(plr)
	if saveData:GetAsync(plr.UserId,level) then
		setAdminLevel(plr.UserId,level)
	elseif plr.UserId == game.CreatorId then
		setAdminLevel(plr.UserId,4)
	end
end)
1 Like

Does it not error if your not the owner?

Also, This looks fine to me.

DataStore:GetAsync() only has one parameter (key). If you’re trying to set level to the data retrieved, then do:

level = saveData:GetAsync(plr.UserId)

After this, check level if level exists in the if/then statement.

1 Like

Try this

local saveData = game:GetService("DataStoreService"):GetDataStore("ezdsf,dkfdsdjsdojdskhdskdnsk")

function setAdminLevel(userid,level)
	local name = game:GetService("Players"):GetPlayerByUserId(userid)
	name.Administrative.AdminLevel.Value = level
	saveData:SetAsync(userid,level)
end

game.Players.PlayerAdded:Connect(function(plr)
	if plr.UserId == game.CreatorId then
		setAdminLevel(plr.UserId,4)
	elseif saveData:GetAsync(plr.UserId,level) then
		setAdminLevel(plr.UserId,level)
	end
end)

this is literally the same code

Nope, I placed the CreatorId condition at the start since if you place the saveData:GetAsync(plr.UserId,level) at the start, then it will obviously not work since everyone will have a save level (if the player has played the game more than once) meaning that your code will only check if your PlayerId is equal to the CreatorId if you still don’t have a saved data. If you place the CreatorId at the start, then the script will always check if the PlayerId is equal to the CreatorId.

1 Like