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

how do i wait for the character to give them the boost?

basically i get an error in output saying " ServerScriptService.PurchaseGamePassServer:3: attempt to index nil with 'Wait' "

game.Players.PlayerAdded:Connect(function(player)
	local Mps = game:GetService("MarketplaceService")
	player.Character:Wait()
	while wait() do	
		if Mps:UserOwnsGamePassAsync(player.UserId, 12663973) then
			player.Character.Humanoid.WalkSpeed = 32
		end
	end
end)

2 answers

✓ Accepted answer

Well, I assume that you are waiting for player's character to be added and not for the character.

What's the fix?

Well, there are two ways to fix it. First method, you can use repeat wait() until loop and wait for the player's character to load in :

repeat wait() until player.Character

Second method, you can wait() for the player's character to load in, using CharacterAdded :

player.CharacterAdded:Wait()

Now, some extra information that can be useful what you are trying to do. Here's the sample script, with all the explanation :

local MP = game:GetService('MarketplaceService')
local Players = game:GetService('Players')
local gamepassId = 123456789 -- An example

Players.PlayerAdded:Connect(function(plr) -- Triggers when the player joins
	plr.CharacterAdded:Connect(function() -- Triggers when the player spawn, or respawns
		local char = plr.CharacterAdded:Wait() -- Ensures that the whole character is loaded
		local hum = char:WaitForChild('Humanoid') -- Gets our humanoid
		
		-- No need for a loop to check
		if MP:UserOwnsGamePassAsync(plr.UserId, gamepassId) then -- Checks whether the player owns
			hum.WalkSpeed = 32 -- Changes the WalkSpeed
		end

		MP.PromptGamePassPurchaseFinished:Connect(function(player, passId, Success) -- It is a function, that triggers when a player purchases a gamepass. The first parameter is the player, second is the gamepassId that he purchased and third is a boolean value, true = Success/false = Cancelled
			if Success then -- If player bought the pass
				if passId == gamepassId then -- If the gamepass purchased have the id, that is stated above in the script
					hum.WalkSpeed = 32
				end
			end
		end)
	end)
end)

Lemme know if it helps!

game.Players.PlayerAdded:Connect(function(player)
    local Mps = game:GetService("MarketplaceService")
    player.CharacterAdded:Wait() -- its CharacterAdded
    while wait() do 
        if Mps:UserOwnsGamePassAsync(player.UserId, 12663973) then
            player.Character.Humanoid.WalkSpeed = 32
        end
    end
end)

3 comments

0

thanks a lot!

0

Add sufficient explanation, don't just write what the user should do. [Look at the other persons answer, and observe]

0

Its in the code the explanation and where he gone wrong

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