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

How can i insert to the table?

Asked by 4 years ago
Edited 4 years ago

Hi, i need to know how can i insert the humanoid walkspeed and jumppower to a table. When i try to concat(print) the output is always blank.

game.ReplicatedStorage.create.OnServerEvent:Connect(function(player, Action)
    local hum = player.Character.Humanoid
    StatsTable = {}
    if Action == "RedEyes" then
        table.insert(StatsTable,hum.WalkSpeed)
        table.insert(StatsTable,hum.JumpPower)
        Ori = player.Character.Head.face.Texture
        player.Character.Head.face.Texture = "http://www.roblox.com/asset/?id=368111294"
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        powa.Value = 1
    end
    if Action == "RedOff" then
        player.Character.Head.face.Texture = Ori
        print(table.concat(StatsTable))
        powa.Value = 0
    end
end)

2 answers

Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
4 years ago

The reason this isn't working is quite simple.

game.ReplicatedStorage.create.OnServerEvent:Connect(function(player, Action)
    local hum = player.Character.Humanoid
    StatsTable = {}
    if Action == "RedEyes" then
        table.insert(StatsTable,hum.WalkSpeed)
        table.insert(StatsTable,hum.JumpPower)
        Ori = player.Character.Head.face.Texture
        player.Character.Head.face.Texture = "http://www.roblox.com/asset/?id=368111294"
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        powa.Value = 1
    end
    if Action == "RedOff" then
        player.Character.Head.face.Texture = Ori
        print(table.concat(StatsTable))
        powa.Value = 0
    end
end)

The two conditional statements check for different conditions. The "Action" parameter cannot be "RedEyes" and "RedOff" at the same time. Consequently, whenever the parameter is the string "RedEyes", the second conditional statement will not run and the table will not be printed. If it does, nothing will be printed, because you haven't added any elements to the table. The only time you add elements is in the first conditional statement's block, where you only add elements and don't print anything. If that block runs, then that means its condition was met, and that the second conditional statement's block will not run.

What I'm saying is that these two blocks of code:

if Action == "RedEyes" then
    table.insert(StatsTable,hum.WalkSpeed)
    table.insert(StatsTable,hum.JumpPower)
    Ori = player.Character.Head.face.Texture
    player.Character.Head.face.Texture = "http://www.roblox.com/asset/?id=368111294"
    hum.WalkSpeed = 32
    hum.JumpPower = 100
    powa.Value = 1
end

if Action == "RedOff" then
    player.Character.Head.face.Texture = Ori
    print(table.concat(StatsTable))
    powa.Value = 0
end

Will never both execute every time the RemoteEvent is fired by the client. Only one of the conditional statements can be met. One of them adds elements, and one of them prints the contents of the table.

To fix it, do this:

local StatsTable = {}

game.ReplicatedStorage.create.OnServerEvent:Connect(function(player, Action)
    local hum = player.Character.Humanoid
    if Action == "RedEyes" then
        StatsTable = {}
        table.insert(StatsTable,hum.WalkSpeed)
        table.insert(StatsTable,hum.JumpPower)
        Ori = player.Character.Head.face.Texture
        player.Character.Head.face.Texture = "http://www.roblox.com/asset/?id=368111294"
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        powa.Value = 1
    end
    if Action == "RedOff" then
        player.Character.Head.face.Texture = Ori
        print(table.concat(StatsTable))
        powa.Value = 0
    end
end)

Setting the StatsTable variable to be empty every time the RemoteEvent is fired will make it so that it is always empty by the time table.concat is called on it, so nothing will be printed. I assume the first conditional statement's block should be clearing the contents and reentering them, and that the second should be printing them if they're already there.

1
Ty! Davviidblox 3 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

instead of using table.concat you could just do it using in pairs i dont always recomend this though

game.ReplicatedStorage.create.OnServerEvent:Connect(function(player, Action)
    local hum = player.Character.Humanoid
    StatsTable = {}
    if Action == "RedEyes" then
        table.insert(StatsTable,hum.WalkSpeed)
        table.insert(StatsTable,hum.JumpPower)
        Ori = player.Character.Head.face.Texture
        player.Character.Head.face.Texture = "http://www.roblox.com/asset/?id=368111294"
        hum.WalkSpeed = 32
        hum.JumpPower = 100
        powa.Value = 1
    end
    if Action == "RedOff" then
        player.Character.Head.face.Texture = Ori
       for i,v in pairs(StatsTable) do
print(v)
end
        powa.Value = 0
    end
end)
0
The issue isn't the method he's using to print the table's contents. Rinpix 639 — 4y

Answer this question