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

Why Command with arguments doesn't work?

Asked by 1 year ago
Edited 1 year ago

Hello I need help beacuse I don't know why this script isn't working.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        local split = message:split(" ")
        if split[1] == "/give" then
            local arg1 = split[2]
            local arg2 = split[3]
            local arg3 = split[4]

            if arg1 and arg2 and arg3 then
                local player = game.Players..split[2]
                local tool = game.ReplicatedStorage..split[3]..split[4]

                tool:Clone(player.Backpack)
            end
        end
    end)
end)

2 answers

Log in to vote
0
Answered by 1 year ago

I found the problem. there is script that works.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        local split = {}
        for word in string.gmatch(message, "%S+") do
            table.insert(split, word)
        end

        if split[1] == "/give" then
            local arg1 = split[2]
            local arg2 = split[3]
            local arg3 = split[4]

            if arg1 and arg2 and arg3 then
                local player = game.Players:FindFirstChild(arg1)
                local tool = game.ReplicatedStorage:FindFirstChild(arg2):FindFirstChild(arg3)

                tool:Clone().Parent = player.Backpack
            end
        end
    end)
end)

Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

There are a couple of issues with the given code:

Fixing the player retrieval issue in the code.

local player = game.Players..split[2] is attempting to concatenate the Players service with the element at index 2 of the split array. This is not allowed in Luau, and would result in a runtime error. To retrieve the player with the specified name, you should use the GetPlayerByName method of the Players service, like this:

local player = game:GetService("Players"):FindFirstChild(split[2])

Fixing the tool retrieval issue in the code.

local tool = game.ReplicatedStorage..split[3]..split[4] is attempting to concatenate the ReplicatedStorage service with the element at index 3 of the split array, and then concatenate the result with the element at index 4 of the split array. This would also result in a runtime error. To retrieve the tool from ReplicatedStorage, you should use indexing, like this:

local tool = game:GetService("ReplicatedStorage")[split[3]][split[4]]

These changes should fix the issues with the code and allow it to work as intended.

Answer this question