Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Gamepass GUI Script Not Having An Effect?

Asked by 5 years ago
Edited 5 years ago

I made a gamepass called FastTravel, With it comes a gui that allows you to teleport to locations within the map, I have the gui working perfectly fine, but when I try to make a script that makes it so only people with the pass can use it, it never works.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local id = 5068130

game.Players.PlayerAdded:connect(function(player)
    if MarketplaceService:UserOwnsGamePassAsync(player.userId, id) then
player:findFirstChild("PlayerGui").FastTravel.Main.Script.Disabled = false 
player:findFirstChild("PlayerGui").FastTravel.Main.Script2.Disabled = true 
else 
player:findFirstChild("PlayerGui").FastTravel.Main.Script.Disabled = true 
player:findFirstChild("PlayerGui").FastTravel.Main.Script2.Disabled = true
end
end)
  • FastTravel is the gui
  • Main is the open/close button
  • Script is the open script
  • Script2 is the close script

The GUI is usable by ALL players with this script active or not. How can i make it so only people that have the gamepass can use the gui?

[EDIT] I have made this script that controls an event named FastTravel

local repStorage = game:GetService("ReplicatedStorage")

local remote = repStorage:WaitForChild("FastTravel")

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local id = 5068130

remote.OnServerEvent:Connect(function(player)
    if MarketplaceService:UserOwnsGamePassAsync(player.userId, id) then
player:findFirstChild("PlayerGui").FastTravel.Main.Script.Disabled = false 
player:findFirstChild("PlayerGui").FastTravel.Main.Script2.Disabled = true 
else 
player:findFirstChild("PlayerGui").FastTravel.Main.Script.Disabled = true 
player:findFirstChild("PlayerGui").FastTravel.Main.Script2.Disabled = true
end
end)

When this event is fired by the script I made and put in the server script service it still does not work. Here is the code for the script

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("FastTravel")
local gamePassID = 5068130

function onPlayerSpawned(player) 
 if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
    remote:FireServer()
else
    print("Does Not Have Pass")
 end
end 

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function()
    onPlayerSpawned(player)
  end)
end)

Players.PlayerSpawned:Connect(onPlayerSpawned)
0
The server cannot access PlayerGui's descendants if they were not placed by the server. And connect, findFirstChild and userId are deprecated, use Connect, FindFirstChild and UserId. User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by
green271 635 Moderation Voter
5 years ago

Client-Server Replication

With FilteringEnabled now mandatory, this is very important. In order to help secure games, there are some restrictions we have to note. First, anything on the client is not replicated to the server. Second, Server Scripts cannot access a player's PlayerGui, unless it parented an object there. Finally, Local Scripts cannot access top-level services such as MarketplaceService, ServerStorage, ServerScriptService, and etc.

The problem here is that you're trying to change the the Player's PlayerGui from the server. In-game, the server cannot see this. If you want to get around this, you can use the OnClientEvent function of RemoteEvent, OR use a RemoteFunction and invoke the client from the server (game.ReplicatedStorage.RemoteFunction.OnClientEvent = functionName)

Deprecation

Deprecation is the act of removing a function, method, etc for a better version. Your code has multiple deprecations in it.

Connect

connect is deprecated, you should be using Connect instead.

FindFirstChild

findFirstChild is deprecated, you should be using FindFirstChild instead.

UserId

userId is deprecated, you should be using UserId instead.

Why does it matter?

Using deprecated code can be harmful to your code. Deprecated code is no longer being updated and may break/be removed at any time.

Ad

Answer this question