传入字符串后尝试将实例与字符串连接时出错
Error attempt to concatenate Instance with string after passing in strings
所以我试图制作一个游戏,在 StarterCharacterScripts
在本地脚本中触摸一个名为 WinPad
的部分以触发一个事件,该事件在聊天中发送消息,并发送PostAsync()
到 Discord webhook。但是,在触发事件时它会给出错误 attempt to concatenate Instance with string
,即使所有传入的参数都是字符串。这是 LocalScript 中的事件触发程序:
local name = tP:FindFirstChild("TowerName")
local diff = tP:FindFirstChild("Difficulty")
local diffid = tP:FindFirstChild("DifficultyId")
local diffcolor = tP:FindFirstChild("DiffColor3")
game.ReplicatedStorage.WinEvent:FireServer(game.Players.LocalPlayer.Name, name.Value, diff.Value, diffid.Value, diffcolor.Value)
这是 OnServerEvent:Connect()
脚本:
game.ReplicatedStorage.WinEvent.OnServerEvent:Connect(function(plr, towername, towerdiff, towerdiffid, color)
local HookData = {
['content'] = "**"..plr.."** has beaten **"..tostring(towername).." [<:"..tostring(towerdiff)..":"..tostring(towerdiffid)..">]**."
}
HookData = http:JSONEncode(HookData)
http:PostAsync(url, HookData)
plr.Parent:MoveTo(Vector3.new(-764, 12.5, -54.5))
createSystemMessage:FireAllClients((plr.." has beaten "..towername.."."), color, Enum.Font.SourceSansBold, Enum.FontSize.Size14)
end)
此外,tP
是被 LocalPlayer.Character.Humanoid
触及的部分
当您使用 RemoteEvents' OnServerEvent signal, the first parameter is always the Player 时,它使用了 FireServer 功能,并且由引擎自动提供。您的函数签名应如下所示:
function(player, plrname, towername, towerdiff, towerdiffid, color)
但是,没有必要提供玩家姓名,因为您可以从 player.Name
中获得。因此,为了解决您的问题并清理您的代码,我会在您的 LocalScript 中推荐它:
local winevent = game.ReplicatedStorage.WinEvent
local name = tP.TowerName.Value
local diff = tP.Difficulty.Value
local diffid = tP.DifficultyId.Value
local diffcolor = tP.DiffColor3.Value
winevent:FireServer(name, diff, diffid, diffcolor)
这在您的服务器脚本中:
local winEvent = game.ReplicatedStorage.WinEvent
winEvent.OnServerEvent:Connect(function(player, towername, towerdiff, towerdiffid, color)
-- format some messages
local message = "%s has beaten %s."
local hookMessage = string.format(message,
"**" .. player.Name .. "**",
string.format("**%s [<:%s:%s>]**", towername, towerdiff, towerdiffid))
local playerMessage = string.format(message, player.Name, towername)
-- fire some data to some server
local HookData = {
['content'] = hookMessage,
}
HookData = http:JSONEncode(HookData)
http:PostAsync(url, HookData)
-- move the player's character somewhere
player.Character:MoveTo(Vector3.new(-764, 12.5, -54.5))
-- alert everyone that stuff has happened
createSystemMessage:FireAllClients(playerMessage, color, Enum.Font.SourceSansBold, Enum.FontSize.Size14)
end)
所以我试图制作一个游戏,在 StarterCharacterScripts
在本地脚本中触摸一个名为 WinPad
的部分以触发一个事件,该事件在聊天中发送消息,并发送PostAsync()
到 Discord webhook。但是,在触发事件时它会给出错误 attempt to concatenate Instance with string
,即使所有传入的参数都是字符串。这是 LocalScript 中的事件触发程序:
local name = tP:FindFirstChild("TowerName")
local diff = tP:FindFirstChild("Difficulty")
local diffid = tP:FindFirstChild("DifficultyId")
local diffcolor = tP:FindFirstChild("DiffColor3")
game.ReplicatedStorage.WinEvent:FireServer(game.Players.LocalPlayer.Name, name.Value, diff.Value, diffid.Value, diffcolor.Value)
这是 OnServerEvent:Connect()
脚本:
game.ReplicatedStorage.WinEvent.OnServerEvent:Connect(function(plr, towername, towerdiff, towerdiffid, color)
local HookData = {
['content'] = "**"..plr.."** has beaten **"..tostring(towername).." [<:"..tostring(towerdiff)..":"..tostring(towerdiffid)..">]**."
}
HookData = http:JSONEncode(HookData)
http:PostAsync(url, HookData)
plr.Parent:MoveTo(Vector3.new(-764, 12.5, -54.5))
createSystemMessage:FireAllClients((plr.." has beaten "..towername.."."), color, Enum.Font.SourceSansBold, Enum.FontSize.Size14)
end)
此外,tP
是被 LocalPlayer.Character.Humanoid
当您使用 RemoteEvents' OnServerEvent signal, the first parameter is always the Player 时,它使用了 FireServer 功能,并且由引擎自动提供。您的函数签名应如下所示:
function(player, plrname, towername, towerdiff, towerdiffid, color)
但是,没有必要提供玩家姓名,因为您可以从 player.Name
中获得。因此,为了解决您的问题并清理您的代码,我会在您的 LocalScript 中推荐它:
local winevent = game.ReplicatedStorage.WinEvent
local name = tP.TowerName.Value
local diff = tP.Difficulty.Value
local diffid = tP.DifficultyId.Value
local diffcolor = tP.DiffColor3.Value
winevent:FireServer(name, diff, diffid, diffcolor)
这在您的服务器脚本中:
local winEvent = game.ReplicatedStorage.WinEvent
winEvent.OnServerEvent:Connect(function(player, towername, towerdiff, towerdiffid, color)
-- format some messages
local message = "%s has beaten %s."
local hookMessage = string.format(message,
"**" .. player.Name .. "**",
string.format("**%s [<:%s:%s>]**", towername, towerdiff, towerdiffid))
local playerMessage = string.format(message, player.Name, towername)
-- fire some data to some server
local HookData = {
['content'] = hookMessage,
}
HookData = http:JSONEncode(HookData)
http:PostAsync(url, HookData)
-- move the player's character somewhere
player.Character:MoveTo(Vector3.new(-764, 12.5, -54.5))
-- alert everyone that stuff has happened
createSystemMessage:FireAllClients(playerMessage, color, Enum.Font.SourceSansBold, Enum.FontSize.Size14)
end)