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

Script doesn't work in studio but works fine in both local and normal servers?

Asked by 6 years ago
Edited 6 years ago

You guys probably know the problem that scripts work in studio, but not in game (FE problems). It could be the same problem but I doubt that... I tried to restart studio and still the same problem, it doesn't give an error!.

EDIT: I think it's because the script is too long and the player joins before the playeradded event is loaded but I'm not sure.

The code that is failing is a playeradded event

game.Players.PlayerAdded:connect(function(player)
    print('ADDED PLAYER')
    for index, admin in pairs(Admins) do
        if admin == player.Name then
            print(player.Name..' is an admin')
            player.Chatted:Connect(function(Message)
                print('admin '..player.Name..' chatted this message: '..Message)
                if Command(Message) then
                    local MsgWords = decrypt(Message)
                    FireCmd(MsgWords, player)
                end
            end)
        end
    end

    UpdateBanlist()
    for index, ban in pairs(BanList) do
        if ban == player.UserId then
            warn(ban..player.Name)
            player:Kick('You have been banned from this game!')
        end
    end
end)

There is a lot more code but it just doesn't print 'ADDED PLAYER' (it doesn't run any code inside the playeradded event).

I searched for it online but I don't think people had the exact same issue, I don't know if this is a script issue or a studio bug.

This is the whole script

local DataNum = 0
local DataStore = game:GetService('DataStoreService')
local BanStore = DataStore:GetDataStore('BanStore_'..DataNum)
local BanNameStore = DataStore:GetDataStore('BanNameStore_'..DataNum)

print('script running')

local ChatEvent = game:GetService('ReplicatedStorage'):WaitForChild('Chat')

local GlobalKey = 1000

local Admins = {'RedcommanderV2', 'BANSA168', 'Player1'} -- Remove Player1
local DecryptionPatterns = {
    ['Default'] = '%S+'
}

local Prefix = '/'

Words = {}
Reason = {}

BanList = {}
BanNameList = {}

function Command(Msg)
    local MsgPrefix = string.sub(Msg,1,1)
    if MsgPrefix == Prefix then
        return true
    else
        return false
    end
end

function decrypt(sentence)
    Words = {}
    for word in string.gmatch(sentence, DecryptionPatterns.Default) do
        table.insert(Words, word)
    end
    return Words
end

function FindTarget(PlrName)
    if PlrName == nil then
        return nil
    end
    if PlrName == 'Everyone' then
        return 'Everyone'
    end
    for index, plr in pairs(game.Players:GetPlayers()) do
        if plr.Name == PlrName then
            return plr
        end
    end
    for index, plr in pairs(game.Players:GetPlayers()) do
        if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then
            return plr
        end
    end

    return nil
end

print('script still running')

function FindTargetId(PlrName)
    if PlrName == 'Everyone' then
        return 'Everyone'
    end

    playerID = nil
    warn('We\'re using FindTargetId so you have to give the full name!')
    local success, message = pcall(function()
        playerID = game.Players:GetUserIdFromNameAsync(PlrName)
    end)

    print(PlrName..' has playerID '..playerID)  

    if success then
        return playerID
    else
        return nil
    end
end

function UpdateBanlist()
    BanList = {}
    BanNameList = {}
    local Bans = BanStore:GetAsync(GlobalKey) or {}
    local Names = BanStore:GetAsync(GlobalKey) or {}

    for index, ban in pairs(Bans) do
        table.insert(BanList, ban)
    end

    for index, name in pairs(Names) do
        table.insert(BanNameList, name)
    end
end

UpdateBanlist()
print('script STILL RUNNING')

local function FindReason(MsgWords, StartNumber)
    Reason = {}
    for index, word in pairs(MsgWords) do
        if index >= StartNumber then
            table.insert(Reason, word)
        end
    end
    return(table.concat(Reason, ' '))
end

function SetCmd(MsgWords, plr)
    local Cmd = MsgWords[1]
    local Target = FindTarget(MsgWords[2]) or plr
    local Value = MsgWords[3] or 'Gold'
    local Amount = MsgWords[4] or 1000

    if Target ~= 'Everyone' then
        local leaderstats = Target:WaitForChild('leaderstats')
        for index, stat in pairs(leaderstats:GetChildren()) do
            if stat.Name == Value then
                stat.Value = Amount
            end
        end
    else
        for index, player in pairs(game.Players:GetPlayers()) do
            local leaderstats = player:WaitForChild('leaderstats')
            for index, stat in pairs(leaderstats:GetChildren()) do
                if stat.Name == Value then
                    stat.Value = Amount
                end
            end
        end
    end
end

print('reached line 138')

function GiveCmd(MsgWords, plr)
    local Cmd = MsgWords[1]
    local Target = FindTarget(MsgWords[2]) or plr
    local Value = MsgWords[3] or 'Gold'
    local Amount = MsgWords[4] or 1000

    if Target ~= 'Everyone' then
        local leaderstats = Target:WaitForChild('leaderstats')
        for index, stat in pairs(leaderstats:GetChildren()) do
            if stat.Name == Value then
                stat.Value = stat.Value + Amount
            end
        end
    else
        for index, player in pairs(game.Players:GetPlayers()) do
            local leaderstats = player:WaitForChild('leaderstats')
            for index, stat in pairs(leaderstats:GetChildren()) do
                if stat.Name == Value then
                    stat.Value = stat.Value + Amount
                end
            end
        end
    end
end

function KickCmd(MsgWords, plr)
    local Cmd = MsgWords[1]
    local Target = FindTarget(MsgWords[2]) or plr
    local Reason = FindReason(MsgWords,3) or ''

    if Target ~= 'Everyone' then
        Target:Kick(plr.Name..' has kicked you because '..Reason)
    end
end

function BanCmd(MsgWords, plr)
    local Cmd = MsgWords[1]
    local Target = FindTarget(MsgWords[2]) or plr
    local Reason = FindReason(MsgWords,3) or ''

    if Target ~= 'Everyone' then
        warn('Banning '..Target.Name)

        if Target.Name == plr.Name then
            warn('Ban failed because the player you are trying to ban is yourself')
        else
            local TargetKey = Target.UserId
            local Bans = BanStore:GetAsync(GlobalKey) or {}
            local Names = BanNameStore:GetAsync(GlobalKey) or {}

            BanList = {}
            BanNameList = {}

            for index, ban in pairs(Bans) do
                table.insert(BanList, ban)
            end

            for index, name in pairs(Names) do
                table.insert(BanNameList, name)
            end

            table.insert(BanList, TargetKey)
            table.insert(BanNameList, TargetKey)

            BanNameList[TargetKey] = Target.Name

            local success, message = pcall(function()
                BanStore:SetAsync(GlobalKey, BanList)
                BanNameStore:SetAsync(GlobalKey, BanNameList)
            end)

            Target:Kick('You have been banned by '..plr.Name..' because '..Reason)
        end

    else
        warn('You cannot ban everyone!')
    end
end

print('reached line 219')

function UnbanCmd(MsgWords, plr)
    local Cmd = MsgWords[1]
    local Target = FindTargetId(MsgWords[2]) or nil

    if Target and Target ~= 'Everyone' then
        warn('unbanning '..Target)
        local TargetKey = Target
        local Bans = BanStore:GetAsync(GlobalKey) or {}
        local Names = BanNameStore:GetAsync(GlobalKey) or {}

        BanList = {}

        for index, ban in pairs(Bans) do
            table.insert(BanList, ban)
        end

        for index, ban in pairs(Bans) do
            if ban == TargetKey then
                table.remove(BanList, index)
            end
        end
        for index, name in pairs(Names) do
            table.insert(BanNameList, name)
        end
        BanNameList[TargetKey] = nil

        for index, name in pairs(BanNameList) do
            if name == TargetKey then
                table.remove(BanNameList, index)
            end
        end

        print(BanList)

        for index, ban in pairs(BanList) do
            print(ban)
        end
        for index, name in pairs(BanNameList) do
            print(name)
        end
        local success, message = pcall(function()
            BanStore:SetAsync(GlobalKey, BanList)
        end)
    elseif Target and Target == 'Everyone' then
        BanList = {}

        local success, message = pcall(function()
            BanStore:SetAsync(GlobalKey, BanList)
            BanNameStore:SetAsync(GlobalKey, BanNameList)
        end)
    end
end

function ListCmd(MsgWords, plr)
    UpdateBanlist()

    ChatEvent:FireClient(plr, MsgWords, Color3.new(1,1,1))
    print('fired')
end

function FireCmd(MsgWords, plr)
    if MsgWords[1] == '/set' then
        SetCmd(MsgWords, plr)

    elseif MsgWords[1] == '/give' then
        GiveCmd(MsgWords, plr)

    elseif MsgWords[1] == '/kick' then
        KickCmd(MsgWords, plr)

    elseif MsgWords[1] == '/ban' then
        BanCmd(MsgWords, plr)       

    elseif MsgWords[1] == '/unban' then
        UnbanCmd(MsgWords, plr)     

    elseif MsgWords[1] == '/list' then
        ListCmd(MsgWords, plr)

    else
        warn('eror!')
    end
end

print('reached line 305')

game.Players.PlayerAdded:connect(function(player)
    print('ADDED PLAYER')
    for index, admin in pairs(Admins) do
        if admin == player.Name then
            print(player.Name..' is an admin')
            player.Chatted:Connect(function(Message)
                print('admin '..player.Name..' chatted this message: '..Message)
                if Command(Message) then
                    local MsgWords = decrypt(Message)
                    FireCmd(MsgWords, player)
                end
            end)
        end
    end

    UpdateBanlist()
    for index, ban in pairs(BanList) do
        if ban == player.UserId then
            warn(ban..player.Name)
            player:Kick('You have been banned from this game!')
        end
    end
end)

print('finished')

Answer this question