Error: Attempt to concatenate string with nil

Hello all! So I am scripting a warning system for a hotel type game but when it comes to setting the text above a players head to something like W1 - Trolling and when I am setting the Warning to be Warning 1 for example, the amountOfWarnings.Value returns nil even after adding 1 to the amountOfWarnings.Value. This then prints: attempt to concatenate string with nil.
Does anyone know how to fix this? Thanks!

Code:

warningGui.WarningMessage.Text = "W" .. amountOfWarnings.Value .. " - " .. reasonForWarn

Is amountOfWarnings an IntValue or NumberValue? If it’s either, it can’t be the reason for the error. Try checking if reasonForWarn has a value attached to it.

It is an IntValue and what do you mean by trying to check is reasonForWarn has a value attached to it?

Can you print all the different values in seperate print statements? This will ensure we know what is nil. Also, how are you defining the 2 things

What 2 things and I will do the printing quick.

amountOfWarnings.Value

and

reasonForWarn

amountOfWarnings.Value is an IntValue and reasonForWarn is an input from a player message which is split using :split

How are they defined in the script? (e.g. amountOfWarnings = …)

Don’t forget to print them out to find what is nil

After adding one to warning amounts, 1 is printed

Can I send the whole script to you via DM? It will make life easier.

If you want. I only got a couple more minutes free though, so you may wish to wait for someone else to be available

Hello, you could try:

warningGui.WarningMessage.Text = "W" .. tostring(amountOfWarnings.Value) .. " - " .. tostring(reasonForWarn)

So you can see what is actually nil.

reasonForWarn is nil charsssssssssssssssssss

(Code was sent to my DMs where I was able to analyse it)

local reasonForWarn = split[warnedPlayerName]

split[] requires an index, not the player’s name

Did you mean split[3]?

I will check quickly (I hate this limmit)

Cannot be split[3] because the reason is more than one word

Hmm. Alright.
This will be a bit of a hacky method, but you could do:

local reasonForWarn = string.sub(msg, #split[1] + #split[2] + 2, #msg)

Edit for anyone coming to this post later:
The code that was an issue was this:

plr.Chatted:Connect(function(msg)
		local split = msg:split(" ")
		
		if plr.Name == "kry1068" and split[1] == ":warn" then
			local warnedPlayerName = split[2]
			local warnedPlayer = game.Players:FindFirstChild(warnedPlayerName)
			local reasonForWarn = split[warnedPlayerName]

Where OP tried to access a point in an array using a name, not an index. OP actually intended for it to be any remaining characters after the split[2]. This was solved by using the string.sub method above.

I am thinking maybe split[3, amount of words] but how would I do that?

Thank you very much :smiley:. By the look of it I still have a lot to learn. Thank you very much!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.