Walkspeed changer only works sometimes

You can write your topic however you want, but you need to answer these questions:
Trying to make a gamepass that gives walkspeed as a bonus. Issue is that this only works sometimes, meaning a player can join a few times and get it and other times it just doesn’t work. This is a local script inside of StarterPlayerScripts

local plr = game.Players.LocalPlayer
repeat task.wait() until plr:FindFirstChild("Loaded") and plr.Loaded.Value

local mps = game:GetService("MarketplaceService")

local function checkVIP()
	task.wait(5)
	local char = plr.Character or plr.CharacterAdded:Wait()
	if mps:UserOwnsGamePassAsync(plr.UserId, 761610949) then
		char.Humanoid.WalkSpeed += 3
	end
	
	if mps:UserOwnsGamePassAsync(plr.UserId, 764589965) then
		char.Humanoid.WalkSpeed += 7
	end
end

plr.CharacterAdded:Connect(checkVIP)
1 Like

This line waits forever. It never fires.

Because you are waiting for the character to be added before you call the function, you can just pass that in as the character and there is no reason to wait for it to load.

local plr = game.Players.LocalPlayer

local mps = game:GetService("MarketplaceService")

local function checkVIP(char)
	task.wait(5)
	if mps:UserOwnsGamePassAsync(plr.UserId, 761610949) then
		char.Humanoid.WalkSpeed += 3
	end

	if mps:UserOwnsGamePassAsync(plr.UserId, 764589965) then
		char.Humanoid.WalkSpeed += 7
	end
end

plr.CharacterAdded:Connect(checkVIP)

Also, I would recommend adding a header section with some options so it is easier to change things in the future, such as GamepassIds and the different WalkSpeeds.

local SLOW_PASS = 761610949
local FAST_PASS = 764589965

local SLOW_SPEED = 3
local FAST_SPEED = 7

Then you could adjust your script and it would look like this:

-- Walkspeed Boost Script (StarterPlayerScripts)

-- Constants
local SLOW_PASS = 761610949
local FAST_PASS = 764589965

local SLOW_SPEED = 3
local FAST_SPEED = 7

-- Variables
local players = game:GetService("Players")
local player = players.LocalPlayer
local mps = game:GetService("MarketplaceService")

-- Functions
local function Check_VIP(char)
	task.wait(5)
	if mps:UserOwnsGamePassAsync(player.UserId, SLOW_PASS) then
		char.Humanoid.WalkSpeed += SLOW_SPEED
	end

	if mps:UserOwnsGamePassAsync(player.UserId, FAST_PASS) then
		char.Humanoid.WalkSpeed += FAST_SPEED
	end
end

-- Initialize
player.CharacterAdded:Connect(Check_VIP)

Hope this helps! :slightly_smiling_face:

1 Like