【问题标题】:Error attempt to concatenate Instance with string after passing in strings传入字符串后尝试将实例与字符串连接时出错
【发布时间】:2021-04-06 12:02:12
【问题描述】:

所以我正在尝试制作一个游戏,在本地脚本中的StarterCharacterScripts 中触摸名为WinPad 的部分以触发事件,该事件在聊天中发送消息,并将PostAsync() 发送到不和谐网络钩子。但是,在触发事件时,它会给出错误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 触及的部分

【问题讨论】:

  • 另外,在 LocalScript 中使用 WaitForChild() 也不起作用。
  • 您说所有都是字符串,但您使用plr 作为表格plr.Parent:MoveTo(Vector3.new(-764, 12.5, -54.5)) 并尝试将其连接到您的HookData['content'] 字符串中。
  • 我添加了 local player = plr 并将 plr.Parent:MoveTo() 更改为 player:MoveTo() 并返回相同的错误。
  • 如果plr 是玩家桌,您需要将['content'] = "**"..plr.."** 更改为能获得名称的东西。
  • 在 LocalScript 中它已经调用了 LocalPlayer 的 Name 属性。

标签: lua webhooks roblox httpservice


【解决方案1】:

当您使用RemoteEvents' OnServerEvent 信号时,第一个参数始终是使用FireServer 功能的Player,并且由引擎自动提供。您的函数签名应如下所示:

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)

【讨论】:

  • 另外,DiffColor3 Color3 值的传入失败,所以我只是做了一个 If 语句,检查传入的难度等于什么,并从存储的值中设置 Color3。
猜你喜欢
  • 2021-04-26
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多