Plr argument not working

Hello, I made this script and somehow it won’t work… How could I fix it? The output is not sending any feedback and everything seems right to me.

local datastore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
local MarketplaceService = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(plr)
	local playerGui = plr:GetService("PlayerGui")
	local PremiumScreen = playerGui.PremiumPurchase
	local data = nil
	data = datastore:GetAsync(plr.UserId) -- getting the data
	
	if data == nil and plr.MembershipType ~= Enum.MembershipType.Premium then
		wait(12)
		PremiumScreen.Frame:TweenPosition(UDim2.new(0.283, 0,0.236, 0))
	else
		--player not new, loading data
	end
end)
1 Like

First of all, I’d recommend using DataStore2 instead of the builtin datastore service, it’s much more reliable. Also, I think that

local playerGui = plr:GetService("PlayerGui")

should be changed to

local playerGui = plr.PlayerGui

PlayerGui’s name should never change.
Also, if this is in a script, I’d recommend changing it to a localscript. That way the position tweening is much smoother.

Also, I believe that :GetAsync() returns nothing, instead of a nil value.
Other than this, I don’t see much wrong.

2 Likes

Wrap your datastore get call into pcall or it may error. You don’t use GetService for PlayerGui, try using :WaitForChild() instead.

Also just set data as the GetAsync thing first, no need to set it nil first because it will be nil already if the player has no data.

If your code is not the full code then you could be yielding too long until after a player has already joined.

Also @IntelligentCappy, not sure if DataStore2 would fix his problem necessarily since he specifically talks about the player instance in his title more than datastore in the post. I was wondering how DataStore2 lets you handle previous values when saving anyways similar to UpdateAsync (if you could tell me).

1 Like

I’m aware that DataStore2 wouldn’t fix the problem, I’m just saying that DataStore2 is better in my opinion

1 Like

Ok, also, I believe :GetAsync() does return nil if theres no data? I have tested this already before.

local success, result = pcall(function()
    return datastore:GetAsync(player.UserId)
end)
if success then
    if result then
        print("Data exists!")
    else
        print("Data does not exist!")
    end
else
    warn("Error fetching data: "..result)
end
1 Like

Awkward. I could’ve sworn that it doesn’t.

1 Like

So how would I need to change my script in order to make it work? Simply change plr:GetService(“PlayerGui”) to plr.PlayerGui?

local playerGui = plr:WaitForChild("PlayerGui")

There is no such service called PlayerGui, use WaitForChild() and you can just call the Roblox Premium modal (see here).

2 Likes

Somehow this is still not working, how can i fix it?:

local datastore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
local MarketplaceService = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(plr)
	local playerGui = plr:WaitForChild("PlayerGui")
	local PremiumScreen = playerGui.PremiumPurchase
	local data = nil
	data = datastore:GetAsync(plr.UserId) -- getting the data
	
	if data == nil and plr.MembershipType ~= Enum.MembershipType.Premium then
		wait(12)
		PremiumScreen.Frame:TweenPosition(UDim2.new(0.283, 0,0.236, 0))
	else
		--player not new, loading data
	end
end)

You should be using a RemoteEvent here to tell the client to show the Gui. Never interact with Guis from the server-side. If this is a client-side script, PlayerAdded isn’t necessary at all and DataStores can’t be used from the client.

It’d also help if you specified what about this isn’t working rather than just saying it doesn’t work. Details should be given so we can better understand what the problem is and how to help you resolve it. As far as it goes now, without information, all I can say is that your issue is rooted towards a bad practice problem in which you’re trying to manipulate Guis from the server.

2 Likes

Change that to

local data = datastore:GetAsync(plr.UserId) or nil