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

Double Coins Gamepass Wrong Values?

Asked by 4 years ago

Hello! In my game, I am trying to make a double coins gamepass. Each round, the player should earn 5 coins (so double coins would give them 10). The gamepass works, and 10 coins are rewarded. However, if you don't own the gamepass, you sometimes receive 5 coins like you are supposed to and other times you receive 15 coins.

  • No errors are given
  • This has only been tested in singleplayer
  • The script only happens once, because the Wins go up only 1. (This is the purpose of having some of the variables) -

Thanks to anyone who attempts to help!

script.Parent.Touched:Connect(function(p)
    local char = p.Parent.Name
    local player = game.Players:FindFirstChild(char)
    local numWins = player.leaderstats.Wins.Value
    local numCoins = player.leaderstats.Coins.Value
    local CoinsEarned = 5

    player.Character.HumanoidRootPart.CFrame = game.Workspace.LOBBYTELE.CFrame

    wait(2)
    local service = game:GetService("MarketplaceService")
    if service:UserOwnsGamePassAsync(player.userId,8519306) == true then
        CoinsEarned = 10
    end
    player.leaderstats.Coins.Value = numCoins + CoinsEarned
    player.leaderstats.Wins.Value = numWins + 1
end)

1 answer

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

This is just a cleanup

local MpS = game:GetService ("MarketplaceService")
local Gamepass = 8519306

local Part = script.Parent

local function PlayerWonAction(Target)
    if not (Target) then return end
    local Player = game.Players:GetPlayerFromCharacter(Target.Parent)
    if (Player and Player.Character) then
        local Leaderstats = Player:FindFirstChild("leaderstats")
        if (Leaderstats and Leaderstats.Coins and Leaderstats.Wins) then
            local CoinsEarned = MpS:UserOwnsGamepassAsync(Player.UserId, Gamepass) and 10 or 5
            Leaderstats.Coins.Value = (Leaderstats.Coins.Value + CoinsEarned)
            Leaderstats.Wins.Value = (Leaderstats.Wins.Value + 1)
        end
        Character:SetPrimaryPartCFrame(workspace.LOBBYTELE.CFrame)
    end
end

Part.Touched:Connect(PlayerWonAction)
0
Thank you! This definitely easier to read than what I had lol :) Amanda314159 291 — 4y
Ad

Answer this question