Warn script error

hello there im working on a script that warns people through a gui i have a error here:

Players.Ziathrius.PlayerGui.ZiaWarn.WarnUI.Warn.WarnScript:20: Expected identifier, got ‘[’

this is the code

script.Parent.MouseButton1Click:connect(function()
    print("Warned User.")
    local target = script.Parent.Parent.WarnUserBox.Text
    local reason = script.Parent.Parent.Reason.Text
    local targetui = game.Players[target].PlayerGui.ZiaWarn.ZiaWarnMsg
    targetui.Reason.Text = reason
    targetui.ZiaWarnMsg.Visible = true
end)

this is made by @Ziathrius

1 Like

Yeah, Its for my admin system.

Any help would be appreciated.

The error message is: Players.Ziathrius.PlayerGui.ZiaWarn.WarnUI.Warn.WarnScript:20: Expected identifier, got ‘[’

I’m assuming this is line 20? The error message doesn’t look like it’s coming from this line.

I’m pretty sure this is fine…

You sure this is line 20?

- Players.Ziathrius.PlayerGui.ZiaWarn.WarnUI.Warn.WarnScript:20: Expected identifier, got ‘[’

The error should be coming from something that is underlined red.

The script has a bunch of Avicii word art at the top, thats why.

here is all the lines of the script:

–███████╗██╗░█████╗░████████╗██╗░░██╗██████╗░██╗██╗░░░██╗░██████╗ ░█████╗░██████╗░███╗░░░███╗██╗███╗░░██╗

–╚════██║██║██╔══██╗╚══██╔══╝██║░░██║██╔══██╗██║██║░░░██║██╔════╝ ██╔══██╗██╔══██╗████╗░████║██║████╗░██║

–░░███╔═╝██║███████║░░░██║░░░███████║██████╔╝██║██║░░░██║╚█████╗░ ███████║██║░░██║██╔████╔██║██║██╔██╗██║

–██╔══╝░░██║██╔══██║░░░██║░░░██╔══██║██╔══██╗██║██║░░░██║░╚═══██╗ ██╔══██║██║░░██║██║╚██╔╝██║██║██║╚████║

–███████╗██║██║░░██║░░░██║░░░██║░░██║██║░░██║██║╚██████╔╝██████╔╝ ██║░░██║██████╔╝██║░╚═╝░██║██║██║░╚███║

–╚══════╝╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░╚═════╝░╚═════╝░ ╚═╝░░╚═╝╚═════╝░╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝

–░██████╗░██╗░░░██╗██╗

–██╔════╝░██║░░░██║██║

–██║░░██╗░██║░░░██║██║

–██║░░╚██╗██║░░░██║██║

–╚██████╔╝╚██████╔╝██║


script.Parent.MouseButton1Click:connect(function()

print(“Warned User.”)

local target = script.Parent.Parent.WarnUserBox.Text

local reason = script.Parent.Parent.Reason.Text

local targetui = game.Players[target].PlayerGui.ZiaWarn.ZiaWarnMsg

targetui.Reason.Text = reason

targetui.ZiaWarnMsg.Visible = true

end)

note: the word art is a bit broken since it has been directly copied from the code

Could it be possible that you’re referencing the target as a string, and not a Player Instance?

Other than that idk why that error is occuring

Try doing local targetui = game.Players:FindFirstChild(target).PlayerGui.ZiaWarn.ZiaWarnMsg. If the error is attempt to index nil with 'PlayerGui', then it means that it can’t find a player which is called target.

That helped a bit, but im now getting this error:

Players.Ziathrius.PlayerGui.ZiaWarn.WarnUI.Warn.WarnScript:20: attempt to index nil with ‘PlayerGui’

Yes. That means there is no player named ‘Target’. You have to input the player’s name 100% correctly (case-sensitive). You can also add an additional check so the script doesn’t break.

script.Parent.MouseButton1Click:connect(function()
    local target = game.Players:FindFirstChild(script.Parent.Parent.WarnUserBox.Text)
    local reason = script.Parent.Parent.Reason.Text
    if not target then
        print("Target not found.")
        return
    end
    print("Target found")
    local targetui = target.PlayerGui.ZiaWarn.ZiaWarnMsg
    targetui.Reason.Text = reason
    targetui.ZiaWarnMsg.Visible = true
end)

Its now saying it found the target and warned them but also saying it couldnt find the target.

That is because of the print you have on the first line (edited). It actually couldn’t find the target. Did you make sure to input everything correctly? Otherwise, I have no idea why it doesn’t work.

I have inputted everything correctly, Ill try to figure it out myself, Thanks for helping with some parts though.

Side note, but connect is deprecated

Also I believe the reason why it’s erroring is cause you’re attempting to find a value of a string inside the Players service, which won’t work

There’s been a post relevant to this I believe, you can check that out or try out this script:

local function GetPlayerFromString(Name)
    print(Name)
    for _, Player in pairs(game.Players:GetPlayers()) do
        print(Player.Name)
        if Player.Name:lower() == Name:lower() then
            print("Found the target")
            return Player
        end
    end
end

script.Parent.MouseButton1Click:connect(function()
    local target = script.Parent.Parent.WarnUserBox.Text
 
    print(typeof(target)) --This would be a string

    local reason = script.Parent.Parent.Reason.Text
    local TargetPlayer = GetPlayerFromString(target)
    print(TargetPlayer)
    if TargetPlayer then
        local TargetGui = TargetPlayer:WaitForChild("PlayerGui")

        TargetGui.Reason.Text = reason
        TargetGui.ZiaWarnMsg.Visible = true
        print("Warned User.")
    else
        print("Not a valid name.")
    end
end)

now im getting: Players.Ziathrius.PlayerGui.ZiaWarn.WarnUI.Warn.WarnScript:31: attempt to index nil with ‘WaitForChild’

Could you try it again? Maybe I messed something up, also added print statements

after adding some prints, i can see that its not finding the target player.

Turns out it was already a target. Just ignore my post.

What do you mean? He referenced it at the start of the function:

Also, could you check to see what the output prints?

can you maybe just add a print(target) after defining target? As in, printing the text.