Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Allow Walkspeed to stay forever after bought?

Asked by 5 years ago

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)
0
If there is any problem I know off bat, I believe the "UserId" has to be "userId"... correct me if I am wrong. BloxRoxe 109 — 5y
0
Also, the PlayerAdded only works when they first join. This doesn't continue when a player dies. BloxRoxe 109 — 5y

2 answers

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

0
Exploiters shouldnt be able to change their speed with FE? TiredMelon 405 — 5y
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. mattscy 3725 — 5y
0
correct^ User#19524 175 — 5y
0
correct^ User#19524 175 — 5y
View all comments (3 more)
0
Oh really? That's good to know! Thanks for the information! TiredMelon 405 — 5y
0
still doesnt work for me? GenericTM 2 — 5y
0
Any errors? If not, add prints to see where it goes wrong. mattscy 3725 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)

Answer this question