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

Need help with error while changing value. Does anyone have a solution to this error?

Asked by
Ghost40Z 118
3 years ago

I am making a name system for my game. It makes a button for every name in the table, and it is supposed to change the value inside the player to the name that is pressed. I get an error saying "invalid argument #3(expected string, got instance)"

localscript inside the gui

wait(2.5)
local Button = game.ReplicatedStorage:WaitForChild("Name")
local Frame = script.Parent
local Names = {"Arya", "Amber", "Imaaad", "Rhiann", "Atlanta", "Aj", "Noa", "Romany", "Layton", "Cruz", "Helen", "Shannay", "Mattew", "Katerina", "Subhaan", "Joesph", "Shaya", "Tymoteusz", "Taliyah", "Mateo", "Aimie", "Irene", "Naomi", "Sorrel", "Ptolemy", "Mathilde", "Nola", "Amari", "Zayd", "Nick", "Pauline", "Adi", "Corbyn", "Lowenna", "Ashwin", "Emilio", "Nathaniel", "Yara", "Aniqa", "Zhen", "Connel", "Henry", "Bob", "Trevor", "Ray", "Suzy", "Devante", "Chantal", "Bryn", "Cariad", "Juliet", "Leila"}
local player = game.Players.LocalPlayer

for i,v in pairs(Names) do
    local copy = Button:Clone()
    copy.Name = v
    copy.Text = v
    copy.Parent = Frame
end

while wait(0.01) do
    for i,v in pairs(Names) do
        local currentbutton = Frame:FindFirstChild(v)
        if currentbutton:IsA("TextButton") then
            currentbutton.MouseButton1Up:Connect(function()
                local ChosenName = currentbutton.Name
                game.ReplicatedStorage.NameEvent:FireServer(player, ChosenName)
                wait(1)
            end)
        end
    end
end

ServerScript that is supposed to change the value

game.ReplicatedStorage.NameEvent.OnServerEvent:Connect(function(player, ChosenName)
    local Name = player:FindFirstChild("PlayerName")
    Name.Value = ChosenName
end)

1 answer

Log in to vote
1
Answered by
notfenv 171
3 years ago

Client

wait(2.5)
local Button = game.ReplicatedStorage:WaitForChild("Name")
local Frame = script.Parent
local Names = {"Arya", "Amber", "Imaaad", "Rhiann", "Atlanta", "Aj", "Noa", "Romany", "Layton", "Cruz", "Helen", "Shannay", "Mattew", "Katerina", "Subhaan", "Joesph", "Shaya", "Tymoteusz", "Taliyah", "Mateo", "Aimie", "Irene", "Naomi", "Sorrel", "Ptolemy", "Mathilde", "Nola", "Amari", "Zayd", "Nick", "Pauline", "Adi", "Corbyn", "Lowenna", "Ashwin", "Emilio", "Nathaniel", "Yara", "Aniqa", "Zhen", "Connel", "Henry", "Bob", "Trevor", "Ray", "Suzy", "Devante", "Chantal", "Bryn", "Cariad", "Juliet", "Leila"}

-- i is unused, but I'm doing this in visual studio code with selene.
for _, v in pairs(Names) do
    local copy = Button:Clone()
    copy.Name = v
    copy.Text = v
    copy.Parent = Frame
end

-- Don't use while loops with connections unless you know what you're doing.
-- It can result in the event being fired >100 times.
--while wait(0.01) do
for _, v in pairs(Names) do
    local currentbutton = Frame:FindFirstChild(v)
    if currentbutton:IsA("TextButton") then
        currentbutton.MouseButton1Up:Connect(
            function()
                local ChosenName = currentbutton.Name
                game.ReplicatedStorage.NameEvent:FireServer(ChosenName)
                wait(1)
            end
        )
    end
end
--end

Server

-- VSC formatting, don't blame me.
game.ReplicatedStorage.NameEvent.OnServerEvent:Connect(
    function(player, ChosenName)
        -- Player is the first argument that is given when a remote event is fired, the rest are arguments YOU fire.
        local Name = player:FindFirstChild("PlayerName")
        Name.Value = ChosenName
    end
)
0
Thanks! It worked lol. Ghost40Z 118 — 3y
Ad

Answer this question