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

Allow Walkspeed to stay forever after bought?

For some reason, this doesnt last after they die? Wouldn't the player added just repeat the script? Im confused, any way to make it work even after they die/respawn?

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 836664  

local function onPlayerAdded(player)
    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        print(player.Name .. " owns the game pass with ID " .. gamePassID)
        player.Character.Humanoid.WalkSpeed = 30
    end
end

game:GetService('Players').PlayerAdded:Connect(onPlayerAdded)

2 comments

0

If there is any problem I know off bat, I believe the "UserId" has to be "userId"... correct me if I am wrong.

0

Also, the PlayerAdded only works when they first join. This doesn't continue when a player dies.

2 answers

You have to do it on CharacterAdded instead of PlayerAdded, as PlayerAdded only fires when the player joins. For example:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character) --this will fire every time a player's character spawns
		character:WaitForChild("Humanoid")
		onPlayerAdded(player)
	end)
end)

Also, userId is deprecated, it should be UserId instead, so that part is fine. You should also keep in mind that exploiters can change their walkspeed at will, so you might want to add some server-sided checks to try and prevent this (such as repeatedly checking how far each player travels from the server).

7 comments

0

Exploiters shouldnt be able to change their speed with FE?

1

Characters are one of the few things that replicate automatically to the server even with FilteringEnabled as they have network ownership. As a result, anything the exploiter does client-sided to influence the character's position will automatically replicate.

0

correct^

0

correct^

0

Oh really? That's good to know! Thanks for the information!

0

still doesnt work for me?

0

Any errors? If not, add prints to see where it goes wrong.

I've added the character added function, but i've still not got it to work?

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 836664  

game.Players.PlayerAdded:Connect(function(player)
	    player.CharacterAdded:Connect(function(character) --this will fire every time a player's character spawns
        character:WaitForChild("Humanoid")
        onPlayerAdded(player)

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        print(player.Name .. " owns the game pass with ID " .. gamePassID)
        player.Character.Humanoid.WalkSpeed = 30
    end
	end)
end)

game:GetService('Players').PlayerAdded:Connect(onPlayerAdded)

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