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

Arithmetic on a string value error. How do I fix this?

Asked by 7 years ago

So, I am attempting to make my game log all chatted commands to my MySQL database through a mixture of PHP, MySQL, and Lua. I have finished the PHP code, but on my game code, I get the following error.

ServerScriptService.ChatManager:7: attempt to perform arithmetic on a string value

local player3 = game.Players.LocalPlayer
local chatMessageEvent = game.ReplicatedStorage.ChatMessage
hs = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        hs:GetAsync("http://url.com/Method=WriteToChatlog&PlayerName=" + player.Name + "&PlayerID=" + tonumber(player.UserId) + "&PlaceID=" + tonumber(game.PlaceId) + "&PlaceName=" + game.Name, true)
    end)
end)
1
Concatenation in Lua is .. not like Java or Python where it's +. M39a9am3R 3210 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You're getting a simple error. You don't concatenate using a + sign. You're supposed to use two dots .. with lua. Here's your script fixed:

local player3 = game.Players.LocalPlayer
local chatMessageEvent = game.ReplicatedStorage.ChatMessage
hs = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        hs:GetAsync("http://url.com/Method=WriteToChatlog&PlayerName=" .. player.Name .. "&PlayerID=" .. tonumber(player.UserId) .. "&PlaceID=" .. tonumber(game.PlaceId) .. "&PlaceName=" .. game.Name, true)
    -- Notice how above I replaced all the + with ..
    -- The reason you got that error was because it thought you were trying to perform a math equation with two strings which doesn't work. 
    end)
end)

Hope I helped, if I did be sure to accept my answer ;)

Also, be sure to read this: Lua String Concatenation

0
I am still used to using +, it's been a while. I have been focusing on C# mostly, thank you for the answer. OfficerPapasman 2 — 7y
0
I was too when I first started. I actually ended up with the same error as you. I'm glad I could help AstrealDev 728 — 7y
Ad

Answer this question