在Roblox Studio中创建手机和拨打朋友

你好,我想知道是否有人知道如何创建一个电话来拨打朋友,例如你可以在电话上看到在线的人员列表,拨打其中一个人并在他回答后与他进行私人聊天...

有什么想法或方式可以实现吗?我已经到处寻找解决方案了,但找不到一个可行的方法...

点赞
用户14769987
用户14769987

首先,在创建聊天功能之前,我们需要一个玩家列表。创建一个框架来充当手机,然后添加以下本地脚本:

local frame = script.parent
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer

for i,v in pairs(plrs) do
    if v.Name ~= plr.Name then
        local optionButton = Instance.new("TextButton")
        optionButton.Text = v.Name
        optionButton.Parent = frame
        optionButton.MouseButton1Click:Connect(function()
            --他们选择要聊天的玩家
            --我们将创建一个值在玩家身上,以便我们可以记住他们正在与谁交谈:
            local val = Instance.new("StringValue")
            val.Name = "chattingTo"
            val.Value = v.Name
            val.Parent = plr
        end)
    end
end

当他们点击该按钮时,我们知道他们想要和玩家 V 聊天。你可以添加一些响铃系统,这样另一位玩家就可以选择接受或拒绝呼叫,但这并不重要。为了在玩家之间进行通信,我们需要一个移除事件(称为“ChatEvent”),以及一个用来显示消息的滚动框架。 在聊天框架下创建一个本地脚本,其中包含以下内容:

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ChatEvent")

event.OnClientEvent:Connect(function(msg, from)
    local textlabel = Instance.new("TextLabel")
    textlabel.Text = from.Name..": "..msg
    textlabel.Parent = script.Parent
end)

以上脚本将处理显示它接收到的消息。现在我们想发送消息,因此在该本地脚本的底部添加以下内容:

local inputBox = script.Parent.myTextBox --obviously change all variables to the route in your game
local sendButton = script.Parent.sendButton
local uis = game:GetService("UserInputService")

function sendChat()
    local msgToSend = inputBox.Text
    local sendTo = game.Players.LocalPlayer.chattingTo.Value
    event:FireServer(sendTo, msgToSend)
    local textlabel = Instance.new("TextLabel")
    textlabel.Text = game.Players.LocalPlayer.Name..": "..msg
    textlabel.Parent = script.Parent
end

sendButton.MouseButton1Click:Connect(sendChat)
uis.InputBegan:Connect(function(input)
     if input.KeyCode == Enum.KeyCode.Return then
         sendChat()
     end
end)

因此,现在无论是点击发送按钮还是按下回车键,都会在服务器上触发一个事件。当客户端上由服务器触发的事件时,它将向聊天添加一个消息。现在我们只需要使用以下脚本将它们连接在一起(服务器):

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ChatEvent")

event.OnServerEvent:Connect(function(from, to, msg)
    --变量 'to' 是我们要发送的对象 - 但它是一个字符串!所以:
    local sendTo = game.Players[to]
    event:FireClient(sendTo, msg, from)
end)

然后(应该)就完成了。这个应用可以工作,但它可能会受到监管行动的影响,因为它没有使用 roblox 的聊天过滤器,因此我建议你集成一些功能以符合要求。

2021-01-09 12:17:10