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

2 scripts that are pretty much identical are working differently?

Asked by
zomspi 541 Moderation Voter
5 years ago
Edited 5 years ago

These 2 scripts are working completely differently while being almost the same? They work the same with the same leaderstat but when the leaderstat is changed one doesn't work?

gamepass script - VIP

local MS = game:GetService("MarketplaceService")
local VIPID = 7187215
local VIPID1 = 7208245

local RS = game:GetService("ReplicatedStorage")
local billboardgui = game:GetService("ServerStorage"):WaitForChild("VIP")

game.Players.PlayerAdded:Connect(function(Player)
 Player.CharacterAdded:Connect(function(Character)
  if MS:UserOwnsGamePassAsync(Player.UserId,VIPID) or MS:UserOwnsGamePassAsync(Player.UserId,VIPID1)  then
    game.ReplicatedStorage.RemoteEvents:WaitForChild("VIPTTRE"):FireClient(Player)
    local clonedgui = billboardgui:Clone()
        clonedgui.TextLabel.Text = "VIP"
        clonedgui.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
        clonedgui.Parent = game.Workspace:WaitForChild(Player.Name).Head

    Player.leaderstats1.Purchases1.Value = 2

  game.Players.PlayerAdded:connect(function(player)
      player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            local tag = character.Humanoid:FindFirstChild("creator")
            if tag ~= nil then
                if tag.Value ~= nil then
                    -- Here tag.Value is the player who killed us, so find their leaderstats
                    local killersLeaderstats = tag.Value:FindFirstChild("leaderstats")
                    if killersLeaderstats then
                        -- Award the cash to the player that killed us
                        killersLeaderstats.Money.Value = killersLeaderstats.Money.Value + 5

                    end
                end
            end
        end)
    end)
end)

  end
 end)
end)

Gamepass script - MVP

local MS = game:GetService("MarketplaceService")

local VIPID1 = 7208245

local RS = game:GetService("ReplicatedStorage")
local billboardgui = game:GetService("ServerStorage"):WaitForChild("VIP")

game.Players.PlayerAdded:Connect(function(Player)
 Player.CharacterAdded:Connect(function(Character)
  if MS:UserOwnsGamePassAsync(Player.UserId,VIPID1)  then
    wait(1)
    local VIP = game.Workspace:WaitForChild(Player.Name).Head:FindFirstChild("VIP")

    VIP.TextLabel.Text = "MVP"
        VIP.TextLabel.TextColor3 = Color3.fromRGB(0, 255, 255)
        if not VIP then
    local clonedgui = billboardgui:Clone()
        clonedgui.TextLabel.Text = "MVP"
        clonedgui.TextLabel.TextColor3 = Color3.fromRGB(0, 255, 255)
        clonedgui.Parent = game.Workspace:WaitForChild(Player.Name).Head
    Player.leaderstats1.Purchases2.Value = 3
        end
end
end)
end)


LocalScript inside button for MVP - Working

local purchases = game.Players.LocalPlayer.leaderstats1.Purchases2

 game.Players.LocalPlayer:WaitForChild("leaderstats1")

purchases.Changed:Connect(function()


    if purchases.Value == 3 then

        print("MVP")

        script.Parent.Visible = true
        script.Parent.Parent.MVP1.TextLabel.Visible = false

    else

        print("Bye")

        script.Parent.Visible = false

    end
end)




Local Script inside button for VIP - Not Working

local purchases = game.Players.LocalPlayer.leaderstats1.Purchases1

 game.Players.LocalPlayer:WaitForChild("leaderstats1")

purchases.Changed:Connect(function()


    if purchases.Value == 2 then

        print("VIP")

        script.Parent.Visible = true
        script.Parent.Parent.VIP1.TextLabel.Visible = false

    else

        print("Bye")

        script.Parent.Visible = false

    end
end)




Right, sorry that was a lot of scripts, I would love it if someone could help!

1 answer

Log in to vote
1
Answered by 5 years ago

Improvements

You dont need three PlayerAdded functions when you can place all your events that relate to this connection under one function

Use GetPropertyChangedSignal() instead of Changed since Changed works on all properties, when you only care if the Value changes.

Use the player's Character instance instead of looking for them in the workspace

Use GetService to retrieve the Players Service

Use WaitForChild on any instance that you are using to make sure it exists, esp. in LocalScripts

Issues

Line 21 on the MVP script only runs when the VIP Gui doesnt exist, you want this to run regardless of whether or not it exists

Revised Server Script (VIP / MVP)

local MS = game:GetService("MarketplaceService")
local VIPID1 = 7187215
local VIPID2 = 7208245

local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("RemoteEvents")

local billboardgui = game:GetService("ServerStorage"):WaitForChild("VIP")

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        if MS:UserOwnsGamePassAsync(Player.UserId, VIPID1) or MS:UserOwnsGamePassAsync(Player.UserId, VIPID2) then
            RE:WaitForChild("VIPTTRE"):FireClient(Player)
            local clonedgui = billboardgui:Clone()
            local ctl = clonedgui:WaitForChild("TextLabel")
            ctl.Text = "VIP"
            ctl.TextColor3 = Color3.fromRGB(255, 255, 0)
            clonedgui.Parent = Character:WaitForChild("Head")

            Player:WaitForChild("leaderstats1"):WaitForChild("Purchases1").Value = 2

            if MS:UserOwnsGamePassAsync(Player.UserId, VIPID2) then
                Player:WaitForChild("leaderstats1"):WaitForChild("Purchases2").Value = 3
            end

            Character:WaitForChild("Humanoid").Died:Connect(function()
                local tag = Character:WaitForChild("Humanoid"):FindFirstChild("creator")
                if tag and tag.Value ~= nil then
                    -- Here tag.Value is the player who killed us, so find their leaderstats
                    local killersLeaderstats = tag.Value:FindFirstChild("leaderstats")
                    if killersLeaderstats then
                        -- Award the cash to the player that killed us
                        local kmoney = killersLeaderstats:WaitForChild("Money")
                        kmoney.Value = kmoney.Value + 5
                    end
                end
            end)
        end
    end)
end)

Revised Local Script (VIP)

local player = game:GetService("Players").LocalPlayer
local purchases = player:WaitForChild("leaderstats1"):WaitForChild("Purchases1")
local button = script.Parent

purchases:GetPropertyChangedSignal("Value"):Connect(function()
    if purchases.Value == 2 then
        print("VIP")
        button.Visible = true
        button.Parent:WaitForChild("VIP1"):WaitForChild("TextLabel").Visible = false
    else
        print("Bye")
        button.Visible = false
    end
end)

Revised Local Script (MVP)

local player = game:GetService("Players").LocalPlayer
local purchases = player:WaitForChild("leaderstats1"):WaitForChild("Purchases2")
local button = script.Parent

purchases:GetPropertyChangedSignal("Value"):Connect(function()
    if purchases.Value == 3 then
        print("MVP")
        button.Visible = true
        button.Parent:WaitForChild("MVP1"):WaitForChild("TextLabel").Visible = false
    else
        print("Bye")
        button.Visible = false
    end
end)
0
That doesn't work zomspi 541 — 5y
Ad

Answer this question