I checked in the wiki, and unless I completely missed it I didn't find anything on giving a player with the gamepass extra walkspeed/health. I added a gamepass like this to my game, and I want to know how to give them the walkspeed unless I want some unhappy customers. Thanks in advance!
How would I award a player with extra walkspeed when they own a gamepass?
2 answers
Mechanics
To do this, you need to take advantage of a few special functions in the Lua API.
The PlayerAdded event.
The PlayerAdded event is a 'listener' code that activates whenever a Player joins the game. Use this to detect when a player enters
The CharacterAdded event.
The CharacterAdded event is similar to the PlayerAdded event, but it activates whenever a Player's Character is added into the workspace. Use this to detect when the player spawns.
The PlayerOwnsAsset method, or function, of
MarketplaceServiceThe PlayerOwnsAsset method checks whether or not the given player owns the given asset ID and returns true if they do, and false if they don't. Most people would tell you to use the PlayerHasPass method of
GamePassService, but it is much more efficient for you to use the former because it returns live results from the website. So say a player bought the GamePass in-game, they would not have to rejoin for it to activate with PlayerOwnsAsset. But since PlayerHasPass returns cached results, from when the player first joined the server, then users will have to rejoin
Code
local passId = 00000 --Your gamepass ID
local speed = 50 --Amount of speed the user will recieve
local ms = game:GetService('MarketplaceService')
--When the player joins
game.Players.PlayerAdded:connect(function(plr)
--When their character spawns
plr.CharacterAdded:connect(function(char)
--Check if they have the pass
if ms:PlayerOwnsAsset(plr,passId) then
--Get their humanoid
local hum = char:WaitForChild('Humanoid')
--Set the speed
hum.WalkSpeed = speed
end
end)
end)
Well, you need to use PlayerHasPass. Considering your description, you'd want it when they respawn as well, so you'd use CharacterAdded as well. I won't give you the script, but I will walk you through making it. First, start off with the PlayerAdded event, followed by CharacterAdded. Within CharacterAdded, have it check if the player owns the gamepass, and do what you wish (Simple property editing). The wiki link I've provided you should help.
This discussion is preserved as it was. New answers, comments, and votes are closed.
