Walkspeed Error After Resetting

Hello, I’ve created a gamepass for increased walkspeed.
The problem is simply that the walkspeed goes back to its default value (16), after resetting or dying.
I have attempted to solve this problem by making a remote function that activates when the player has died and checks if they have the gamepass, however it doesn’t keep the walkspeed after dying. Any help is appreciated.

local script:

local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 11351222
local remoteFunction = game.ReplicatedStorage.Walkspeed
local playerHasGamepass = remoteFunction:InvokeServer()

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(playerHasGamepass)
		if playerHasGamepass == true then
			character:WaitForChild("Humanoid").Walkspeed = 30
		end
	end)
end)

Server script:

local remotefunction = game.ReplicatedStorage.RemoteFunction
local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 11351222
local hasPass = false

remotefunction.OnServerInvoke = function(player)
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	if hasPass == true then
		return true
	end
end
1 Like

i am not sure if this will work though but try making it reset on spawn like guis

That’s a good idea :slight_smile: Thank you.

1 Like

There’s no real need for the LocalScript that you have above there. All of this can be done from the server.

What I would suggest for a server script:

local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 11351222

function onPlayerAdded(player)
    player.CharacterAdded:Connect(function()
        if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
            repeat wait() until player.Character
            player.Character:WaitForChild("Humanoid").WalkSpeed = 30
        end
    end
end

It’s as simple as that. No action needed from the client at all.

3 Likes

This will not work because you did not call the function and forgot and ) on line 9. Also, no need to do repeat wait() until player.Character as you already did a character added event.

local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 11351222

function onPlayerAdded(player)
    player.CharacterAdded:Connect(function(char)
        if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
            char:WaitForChild("Humanoid").WalkSpeed = 30
        end
    end)
end

game.Players.PlayerAdded:Connect(function(player)
	onPlayerAdded(player)
end)
1 Like

Oh you’re right, my bad. Simple oversights.

1 Like

No worries! We all make mistakes.