I created a gamepass called “Speed GamePass”, and it works. But whenever the player leaves the game or resets… They will loose their speed. How can I fix this? Any response is appreciated!
Code:
local speedGamePassImageButton = script.Parent
local speedGamePassLabel = script.Parent.SpeedGamePassLabel
local marketPlace = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local gamePassID = 15190401
speedGamePassImageButton.MouseButton1Up:Connect(function()
marketPlace:PromptGamePassPurchase(player, gamePassID)
marketPlace.PromptGamePassPurchaseFinished:Connect(function(player, ID, wasBought)
if wasBought and ID == gamePassID then
player.Character:WaitForChild("Humanoid").WalkSpeed = 200
speedGamePassLabel.Text = "Successfully bought speed gamepass!"
wait(3)
speedGamePassLabel.Text = "If you buy the speed gamepass, you will be granted extra speed!"
end
end)
end)
game.Players.PlayerAdded:Connect(function(player)
local hasSpeedGamePass = marketPlace:UserOwnsGamePassAsync(player.UserId, gamePassID)
if hasSpeedGamePass then
player.Character:WaitForChild("Humanoid").WalkSpeed = 200
end
end)
This is because you are only utilizing PlayerAdded. You should nest a CharacterAdded event in that PlayerAdded event so that every time the character respawns, it will get the speed boost.
Hello! You can use the Player.CharacterAdded event so everytime the character loads you’ll change the WalkSpeed. Would be something like this: (Haven’t tested)
game.Players.PlayerAdded:Connect(function(player)
local hasSpeedGamePass = marketPlace:UserOwnsGamePassAsync(player.UserId, gamePassID)
if hasSpeedGamePass then
--Make a characterAdded conection
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.WalkSpeed = 200
end)
end
end)
Try debugging the script, such as putting prints so you know if the connections ever fire, try seeing what’s the Humanoid.WalkSpeed right after the connection triggers.
game.Players.PlayerAdded:Connect(function(player)
local hasSpeedGamePass = marketPlace:UserOwnsGamePassAsync(player.UserId, gamePassID)
if hasSpeedGamePass then
print(player.Name .. " owns the gamepass")
--Make a characterAdded conection
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.WalkSpeed = 200
end)
end
end)
That means the hasSpeedGamePass boolean is not true, which means the connection will never be made. Check if the GamepassID is correct and if you are using the correct reference for MarketPlaceService, you can also check if hasSpeedGamePass is actually “false” and not nil.
Update: I tested the same script in-game and it worked completly fine for me.
local speedGamePassImageButton = script.Parent
local speedGamePassLabel = script.Parent.SpeedGamePassLabel
local marketPlace = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
local gamePassID = 15190401
speedGamePassImageButton.MouseButton1Up:Connect(function()
marketPlace:PromptGamePassPurchase(player, gamePassID)
marketPlace.PromptGamePassPurchaseFinished:Connect(function(player, ID, wasBought)
if wasBought and ID == gamePassID then
print(marketPlace:UserOwnsGamePassAsync(player.UserId, gamePassID))
player.Character:WaitForChild("Humanoid").WalkSpeed = 200
speedGamePassLabel.Text = "Successfully bought speed gamepass!"
wait(3)
speedGamePassLabel.Text = "If you buy the speed gamepass, you will be granted extra speed!"
end
end)
end)
game.Players.PlayerAdded:Connect(function()
local hasSpeedGamePass = marketPlace:UserOwnsGamePassAsync(player.UserId, gamePassID)
if hasSpeedGamePass then
print(player.Name .. " owns the gamepass")
--Make a characterAdded conection
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.WalkSpeed = 200
end)
end
end)
Oh wait I think I know the problem. I’m not sure if this will run any different then what you already did but on my game it works, so maybe it will here.
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:connect(function(char)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players[char.Name].UserId, id) then
char.Humanoid.WalkSpeed = 50
end
end)
end)
What you did was you made a local Boolean variable that checks if the user owns the gamepass, and then using that value you made an if statement. In this script, if they own the gamepass then it instantly runs the script.