Hello fellow devs!
Currently I am developing a role play game called Samurai Era Wonderland.
In this experience, there is an immigration office where a player answer some questions to get a travel permit. I want the player is kicked from the game when answering wrongly.
For that, I made the following 3 scripts:
The module script for dialog
Blockquote
local Info = {}
Info.LeaveMessage = “Goodbye, I’ll see you around.”
Info.CameraDistance = 3
Info.Dialog = {
[1] = {
Text = “Hello, we are responsible for security of this wonderland. Do you need a travel permit?”;
Choices = {
–[1] = { Text = “Yes”; };
[1] = { Text = “Yes”; Next = 3 };
[2] = { Text = “No”; Next = 4};
}
};
–[2] = { Text = “Im sorry to hear that”; };
[3] = {
Text = “Then answer some questions properly. Otherwise you shall be evicted from this land immediately.”…
" Firstly, do you have any weapons with you?";
Choices = {
[1] = { Text = "Yes"; Next = 5; Follow = false };
[2] = { Text = "No"; Next = 6; Follow = false };
--[4] = { Text = "Nice weather, right?"; Next = 7; Follow = true };
}
};
[4] = {
Text = "Without the travel permit, you are allow to visit some checkpoints only.";
Choices = {}
};
[5] = {
Text = "Then, you shall be evicted.";
Choices = {}
};
[6] = {
Text = "Great. Next, Will you respect local lives?";
Choices = {
[1] = { Text = "Yes"; Next = 7; Follow = false };
[2] = { Text = "No"; Next = 5; Follow = false };
--[4] = { Text = "Nice weather, right?"; Next = 7; Follow = true };
}
};
[7] = {
Text = "OK, then, I will give you the travel permit.";
Choices = {}
};
}
return Info
Blockquote
The server script
Blockquote
local replicated = game:GetService(“ReplicatedStorage”)
local event = replicated:WaitForChild(“RemoteEvents”).TalkEvent
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA(“Model”) and v:FindFirstChild(“Humanoid”) then
local hrp = v:WaitForChild(“HumanoidRootPart”)
local head = v:WaitForChild(“Head”)
local module = require(v.Info)
local prompt = Instance.new("ProximityPrompt", hrp)
prompt.ActionText = "Talk"
prompt.ObjectText = v.Name
prompt.RequiresLineOfSight = false
prompt.MaxActivationDistance = 25
prompt.Triggered:Connect(function(player)
if module.CameraDistance then
event:FireClient(player, v, module, CFrame.new(head.Position + head.CFrame.LookVector * module.CameraDistance, head.Position))
else
event:FireClient(player, v, module)
end
end)
end
end
Blockquote
The client script
Blockquote
local players = game:GetService(“Players”)
local player = players.LocalPlayer
local tweenService = game:GetService(“TweenService”)
local replicated = game:GetService(“ReplicatedStorage”)
local event = replicated:WaitForChild(“RemoteEvents”).TalkEvent
local camera = workspace.CurrentCamera
local main = script.Parent.Main
local choicesFrame = main.ChoicesFrame
local nameLabel = main.NameFrame.Label
local dialogLabel = main.DialogFrame.Label
local choiceTemplate = script.ChoiceTemplate
local currentNpc = nil
local storedChoices = {}
local function typeWrite(text, label)
label.Text = “”
for i, v in text:split("") do
label.Text = label.Text .. v
task.wait(0.05)
end
end
local function endDialog(info)
for i, child in pairs(choicesFrame:GetChildren()) do
if child:IsA(“TextButton”) then
child:Destroy()
end
end
typeWrite(info.LeaveMessage, dialogLabel)
task.wait(0.5)
currentNpc.HumanoidRootPart.ProximityPrompt.Enabled = true
camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = game.Players.LocalPlayer.Character
main:TweenPosition(UDim2.new(0,0,1,0))
task.wait(1)
main.Visible = false
nameLabel.Text = ""
dialogLabel.Text = ""
storedChoices = {}
currentNpc = nil
end
local function nextDialog(info, lastChoice)
for i, child in pairs(choicesFrame:GetChildren()) do
if child:IsA(“TextButton”) then
child:Destroy()
end
end
local dialog
if lastChoice then
if lastChoice.Next then
dialog = info.Dialog[lastChoice.Next]
else
endDialog(info)
end
else
dialog = info.Dialog[1]
end
if not dialog then return end
typeWrite(dialog.Text, dialogLabel)
if dialog.Text == "Then, you shall be evicted." then
task.wait(1)
player:Kick()
return
end
if dialog.Choices and (#dialog.Choices > 0 or #storedChoices > 0) then
for i, choice in pairs(storedChoices) do
local clone = choiceTemplate:Clone()
clone.Parent = choicesFrame
clone.Name = choice.Text
clone.Label.Text = choice.Text
clone.MouseButton1Click:Connect(function()
storedChoices[i] = nil
nextDialog(info, choice)
end)
end
for i, choice in dialog.Choices do
local clone = choiceTemplate:Clone()
clone.Parent = choicesFrame
clone.Name = choice.Text
clone.Label.Text = choice.Text
if choice.Follow == true then
storedChoices[i] = choice
end
clone.MouseButton1Click:Connect(function()
storedChoices[i] = nil
nextDialog(info, choice)
end)
end
local leave = choiceTemplate:Clone()
leave.Parent = choicesFrame
leave.Name = "Leave"
leave.BackgroundColor3 = Color3.fromRGB(77, 35, 36)
leave.Label.Text = "I have to go"
leave.MouseButton1Click:Connect(function()
endDialog(info)
end)
else
task.wait(1)
if dialog.Next then
nextDialog(info, dialog)
else
endDialog(info)
end
end
end
event.OnClientEvent:Connect(function(npc, info, cameraCF)
if currentNpc == nil and npc then
currentNpc = npc
nameLabel.Text = npc.Name
dialogLabel.Text = ""
storedChoices = {}
for i, child in pairs(choicesFrame:GetChildren()) do
if child:IsA("TextButton") then
child:Destroy()
end
end
if cameraCF then
npc.HumanoidRootPart.ProximityPrompt.Enabled = false
camera.CameraType = Enum.CameraType.Scriptable
tweenService:Create(workspace.CurrentCamera, TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {CFrame = cameraCF}):Play()
end
main.Position = UDim2.new(0,0,1,0)
main.Visible = true
main:TweenPosition(UDim2.new(0,0,0,0))
task.wait(1)
nextDialog(info, nil)
end
end)
Blockquote
In nextDialog function of the last local script, I put Kick().
When I test, it seems work in Studio as follows:
However, in the real game, when I click the text button of “Yes”,
the sceen simply freeze.
I appreciate if someone help me.