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

How Come One Of My Conditionals Never Runs In My Chat Command Script?

Asked by 3 years ago
Edited 3 years ago

I'm making Chat Commands and recently I've added a new one. This Command is meant to speed up the targeted player by adjusting the walk speed of the humanoid. However, although the Speed All Part works, targeting a specific player (via name) never does. To Be Able To Convert the string to an actual number, I use the tonumber() method. Help Appreciated!


Edit: I updated the script to what I currently have


Full Code

local Valid_Commands_Table = require(game:GetService("ServerStorage").ValidCommands)
local Permitted_Players = require(game:GetService("ServerStorage").PlayerNamesPermitted)
local ToWarnEvent = game:GetService("ReplicatedStorage").ToWarn
local Maximum_Amount_Of_Warns = 3
function Check(Plr)
    for _, Permitted_Player in pairs(Permitted_Players) do
        if Plr.Name == Permitted_Player then
            return true
        else
            return false
        end
    end
end
game:GetService("Players").PlayerAdded:Connect(function(Player)
    local Warns_Num_Value = Instance.new("NumberValue")
    Warns_Num_Value .Parent = Player
    Warns_Num_Value.Name = "Warns"
    Player.Chatted:Connect(function(StringMsg)
        local SplitString = StringMsg:split(" ")
        if Check(Player) and SplitString[1] == Valid_Commands_Table[1] then --Kick Command
            local NameOfPlayer = SplitString[2]
            local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfPlayer)
            if ActualPlayer then
                local Reason = StringMsg:split(NameOfPlayer)[2]
                ActualPlayer:Kick(Reason)
            end
        elseif Check(Player) and SplitString[1] == Valid_Commands_Table[2] then
            local Name_Of_Player = SplitString[2]
            local PlayerToWarn = game:GetService("Players"):FindFirstChild(Name_Of_Player)
            local Reason = StringMsg:split(Name_Of_Player)[2]
            local WarnsInPlayer = PlayerToWarn:WaitForChild("Warns")
            if WarnsInPlayer then
                WarnsInPlayer.Value += 1
                ToWarnEvent:FireClient(PlayerToWarn)
                if WarnsInPlayer.Value >= 3 then
                    PlayerToWarn:Kick("Max Number Of Warns Reached, therefore you have been kicked from the server.")
                end
            end
        elseif Check(Player) and SplitString[1] == Valid_Commands_Table[3] then
            local Targets = SplitString[2]
            if Targets == "All" then
                for _, TargetedPlayer in pairs(game:GetService("Players"):GetChildren()) do
                    local Char_Of_Plr = TargetedPlayer.Character
                    local HumanoidOfTP = Char_Of_Plr.Humanoid
                    HumanoidOfTP:TakeDamage(HumanoidOfTP.MaxHealth)
                end
            else
                local NameOfTargetedPlr = SplitString[2]
                local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfTargetedPlr)
                if ActualPlayer then
                    ActualPlayer.Character.Humanoid:TakeDamage(ActualPlayer.Character.Humanoid.MaxHealth)
                end
            end
        elseif Check(Player) and SplitString[1] == Valid_Commands_Table[4] then
            local Target = SplitString[2]
            if  not Target == "All" then
                local NameOfPlr = SplitString[2]
                local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfPlr)
                if ActualPlayer then
                    ActualPlayer.Character:WaitForChild("Humanoid").WalkSpeed = tonumber(StringMsg:split(NameOfPlr)[2])
                end
            elseif Target == "All" then
                for _, plr in pairs(game:GetService("Players"):GetChildren()) do
                    plr.Character.Humanoid.WalkSpeed = tonumber(StringMsg:split(Target)[2])
                end
            end
        end
    end)
end)

Part That Doesn't Run

    local Target = SplitString[2]
            if  not Target == "All" then
                local NameOfPlr = SplitString[2]
                local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfPlr)
                if ActualPlayer then
                    ActualPlayer.Character:WaitForChild("Humanoid").WalkSpeed = tonumber(StringMsg:split(NameOfPlr)[2])
                end
0
You're not letting the Check function fully evaluate each expression; let it iterate through each "Permitted Player" until** it finds a match. Ziffixture 6913 — 3y
0
k so i dont do the return Plr.Name == plrPermitted, but instead make it check if it matches a player, and if it does then i return true? JeffTheEpicRobloxian 258 — 3y
0
ill try that JeffTheEpicRobloxian 258 — 3y
0
nvrmind it didnt work JeffTheEpicRobloxian 258 — 3y
View all comments (4 more)
0
Of course it didn't, you're still not letting it traverse. A return statement cancels the scope. Use "return false" outside of the enumeration, that way, the logic states that if no match was found, it'll give a false impression. Ziffixture 6913 — 3y
0
Alr JeffTheEpicRobloxian 258 — 3y
0
NEVER use PlayerName. It's unsecure and if someone changesa their username it won't work. Use Player.UserId instead nekosiwifi 398 — 3y
0
Alr Ill change that then JeffTheEpicRobloxian 258 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Hey JeffTheEpicRobloxian!

This is a good question, and I hope I can help you with your problem. From what I can tell by your code, you are not letting the Check function fully traverse or iterate.

Below is an example of what your code should look like...

function Check(Plr)
    for _, Permitted_Player in pairs(Permitted_Players) do
        if Plr.Name == Permitted_Player then
            return true
        end
    end
    return false
end

Hope this helps!

0
Thanks! JeffTheEpicRobloxian 258 — 3y
Ad

Answer this question