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
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.
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)