Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to enable a certain GUI if the player has a game pass?

So I've been working really hard and i just can't figure out how to enable a certain GUI if the player has a gamepass? here's my script


local plr = game.Players.LocalPlayer

local gamepassid = 5509969
game.Players.PlayerAdded:Connect(function()
	while wait(0.2) do 
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync (plr.UserId, gamepassid) and print("Got gamepass") then
	game.ReplicatedStorage.Events.enabler:FireServer()
	
end
end
end)

here's the onserverevent script


local plr = game.Players.LocalPlayer
game.ReplicatedStorage.Events.enabler.OnServerEvent:Connect(function()
	
	plr.PlayerGui.Trail2onoff.Enabled = false
	script.Parent.Trail2onoff.Enabled = false
end)

I would really appreciate your help! Also there is no error

1 comment

0

Put a print("it worked") below the onserverevent line and see if it prints, also put one after the fireserver line. That will tell us if the event is firing, and if the server is seeing it.

1 answer

Here are some very important things to remember whilst scripting:

  • You can only access LocalPlayer (game.Players.LocalPlayer) from a LocalScript.
  • You cannot edit a players GUI from a server script, as you have done from the "onserverevent script" (this is wrong). You can only edit GUI from a LocalScript.

With your script, you have completely messed up here so I'm just gonna rewrite it and consequently, as a bonus, give you good ways to organise scripts!

SERVER SCRIPT:

local MarketPlaceService = game:GetService("MarketPlaceService")
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- create a RemoteEvent first!
local GamePasses = {
	GamePassNameHere = 5509969
	-- insert other game passes here
}
local GamePassNameHere = {}
-- create more local variables like this for each gamepass


game.Players.PlayerAdded:Connect(function(player)
	local UserId = player.UserId
	
-- retrieve the data here THEN continue to doing the gamepass stuff	

	GamePassNameHere[UserId] = MarketPlaceService:UserOwnsGamePassAsync(UserId,GamePasses.GamePassNameHere)
	
if GamePassNameHere[UserId] then
    		RemoteEvent:FireClient(player,"GamePassNameHere")
	end
end

CLIENT SCRIPT (LOCAL SCRIPT):

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

RemoteEvent.OnClientEvent:Connect(function(tag)
	print("Remote Event Received: "..tag) -- telling you that you received the event
	if tag == "GamePassNameHere" then
		playerGui.Trail2onoff.Enabled = false -- whatever gui you want to enable/disable
		playerGUI.moreguihere.Frame.Visible = true
		-- etc

	elseif tag == "you can write more event's here if you want" then
		-- do stuff
	end
end)

And that's it! Leave me an upvote or whatever if it was a good answer :) and if you see an error and/or have any questions i'll be glad to edit/answer.

3 comments

0

It's was a good answer for a while but i came upon a error and here it is

0

10:19:04.498 - ServerScriptService.Event:16: attempt to index local 'UserId' (a number value) 10:19:04.499 - Stack Begin 10:19:04.499 - Script 'ServerScriptService.Event', Line 16 10:19:04.499 - Stack End

0

This is the fixed line, just missed a comma i believe, tell me if there are still any errors (also edited the answer). GamePassNameHere[UserId] = MarketPlaceService:UserOwnsGamePassAsync(UserId,GamePasses.GamePassNameHere)

This discussion is preserved as it was. New answers, comments, and votes are closed.