So I’m trying to add a Premium benefit for players if they play my game. It’s just a simple speed boost that’s 4 times the default WalkSpeed. The sprint toggle is an ImageButton within a ScreenGUI. In my code, I check if the player owns Premium, then if they do, I make the button visible to them so they can use it.
Since I don’t have Premium, I added a Premium Purchase Modal in my game and “bought” it while I was testing. After I “bought” Premium, the button did not show up. I tried resetting my character, but no luck.
Here’s the code, located in a Script within the ServerScriptService:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
if player.MembershipType == Enum.MembershipType.Premium then
game.StarterGui.premiumbenefits.walkspeed.Visible = true -- "premiumbenefits" is the name of the ScreenGUI, and "walkspeed" is the name of the ImageButton.
end
end)
Thank you for reading - any ideas as to what might be causing the problem?
You can’t use StarterGui to edit what a player sees in the game. StarterGui is pretty much where the server stores GUI objects. Then, once a player joins the game, everything inside the StarterGui gets copied into Player.PlayerGui, which is how the player will see things. To edit the GUI on a player-to-player basis, make a LocalScript inside the GUI which can access Players.LocalPlayer, and then check their membershiptype. If premium, you can enable the GUI, and if not premium, you can destroy it.
You should run the check if player owns premium once your done buying since what your code is doing is just checking if you own premium whenever your added, i dont really know what “Premium Purchase Model” is but you get my point.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
if player.MembershipType == Enum.MembershipType.Premium then
local pg = player:WaitForChild("PlayerGui")
pg.premiumbenefits.walkspeed.Visible = true -- "premiumbenefits" is the name of the ScreenGUI, and "walkspeed" is the name of the ImageButton.
end
end)
You were only making the Gui In StarterGui Visible for all future players connecting, you needed to mention PlayerGui to only change it for that user in that server. This needs to be a server script placed in Workspace or ServerScript Service. Still haveing problems oddly? Head over to developer.roblox.com or CKStudio +
So I put a LocalScript into the Button and wrote the following code into it:
local Players = game:GetService("Players")
Players.LocalPlayer:Connect(function(player)
if player.MembershipType == Enum.MembershipType.Premium then
player:WaitForChild("PlayerGui").premiumbenefits.walkspeed.Visible = true
end
end)
And it doesn’t work. So I moved the LocalScript into the ScreenGUI and it still didn’t work. Thanks for helping though!
LocalScript in the ScreenGUI with the following code:
local Players = game:GetService("Players")
Players.LocalPlayer:Connect(function(player)
if player.MembershipType == Enum.MembershipType.Premium then
script.Parent.walkspeed.Visible = true
end
end)