Kick() seems work in Studio, but not in the real experience

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.

1 Like

it doesn’t look like its working in studio. It should have something in red along the lines of “Player has been kicked with the following message:” or something.


Also, when you put code, contain it within ```.

e.g.

```
print(“Hello World”)
```
so that the code is actually readable

print("Hello World")
2 Likes

Was about to say; some wasn’t put in something like:

print(“Hello World”)
2 Likes

Thank you for your quick reply.
I try to repost the scripts.

Module

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

Server Script

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

Local Script

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)
1 Like

You should put the “Kick” function in the server script not in the local script.

3 Likes

Thank you for your replay.
I checked the official document for Kick () to find Kick() can be used in a local script to kick a local player.

2 Likes

Oh I didn’t know that. You are right. But as @SeargentAUS mentioned it doesn’t seem like your “Kick” function is working well in Roblox Studio. Can you show us how it works in Roblox Studio by recording the video?

2 Likes

Yes, as you mention, I found it is not working well in Studio too. Anyway I want to know how I can solve the problem.

1 Like

Can you put a print statement inside of the kick section to see if it even runs?

2 Likes

I put a print statement in the function as below.
At least it runs.

But what does “When using this method from a LocalScript, only the local user’s client can be kicked.” actually mean?
I’m no expert on this, but it seems if it’s works in Studio (where everything runs Client based because it’s running just on your computer) but doesn’t work in game, then you’ve got an issue with the difference between Client and Server issues.
Can’t you run the Kick script from the server so players can’t access it locally on their computers?

2 Likes

Thank you for your reply. I have made a remote event to kick a player and wrote Kick() in the server script. Now it works. I am not sure what Roblox want to say about Kick() used in a local script, but anyway it works now. Thank you for all of your cooperation, guys! Happy developing!

1 Like

Yeah, from that vague documentation it sounds like it should work, but maybe it only kicks them on the client but the client has the ability to spawn back in?
Like I said, I’m no expert but a better description like ‘Kick will only remove the player from the game if it is an server script or uses a remote event’ would be more descriptive.

I find their documentation is kind of based on a ‘you should know x amount of scripting and terminology to understand this sentence’.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.