Kicking a player and creating a fade frame on touch

I’m trying to create a script that makes a frame tween (by making it fade out) and then kicks the player after it’s done tweening only if the player touches a part.
When I test the game and touch the part, nothing happens. (Not even errors in output)
Here is my script and explorer/hierarchy:

local TweenService = game:GetService("TweenService")
local FadeFrame = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("FadeGui").FadeFrame
	local function OnTouch(Hit)
		if Hit.Parent:FindFirstChildOfClass("Humanoid") then
		local fadeInTween = TweenService:Create(FadeFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0), {BackgroundTransparency = 0})
		fadeInTween:Play()
		wait(1)
		game:GetService("Players").LocalPlayer:Kick("An unkown error occured.")
	end
end
script.Parent.Touched:Connect(OnTouch)

Screen Shot 2023-09-03 at 5.53.43 PM
Screen Shot 2023-09-03 at 5.53.58 PM
I’ve looked through the devforum and I couldn’t find anything that helps with both tweening and kicking. I’ve tried using a server script (Script) but I realized using that wouldn’t allow me to use LocalPlayer.

3 Likes

LocalScripts don’t work in Workspace. I would move your LocalScript to under your FadeFrame and paste this inside:

--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Part = workspace.Part --//Change if needed
local FadeFrame = script.Parent

--//Functions
Part.Touched:Connect(function(hit)
	if hit.Parent ~= LocalPlayer.Character then
		return
	end
	
	local fadeInTween = TweenService:Create(FadeFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 0})
	fadeInTween:Play()
	fadeInTween.Completed:Wait()
	
	LocalPlayer:Kick("An unknown error occured.")
end)

LocalScripts don’t run in the workspace. Change the script’s location to the GUI instead.
An example:

local TweenService = game:GetService("TweenService")
local Part = game.Workspace:WaitForChild("Part") -- The part that you want to detect the touches on
local FadeFrame = script.Parent.FadeFrame -- The fade frame's location

Part.Touched:Connect(function(hitPart: BasePart) -- Detects if another part has touched the part
	if not hitPart.Parent or not hitPart.Parent:FindFirstChild("Humanoid") then return end -- Checking if the hitPart belongs to a character
	local FadeInTween = TweenService:Create(FadeFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0), {BackgroundTransparency = 0}) -- Creating the tween
	FadeInTween:Play() -- Playing the tween
	FadeInTween.Completed:Wait() -- Waiting for the tween to end
	
	game.Players.LocalPlayer:Kick("An unknown error has occured.") -- Kicking the player
end)

This would also fire if another player touched it.

So, would this mean if one player touched it, everyone in the entire server would be kicked? If so, how do I fix it so only the player that touches it is kicked and only their gui is tweened?

Mine already does what you wanted by checking if the LocalPlayer touched it. Did mine not work? Or did you only try the second reply for some reason?

Oh, I’m so sorry I didn’t see your reply but only the second. I just fixed it by using LocalPlayer:Kick(). So, if I’m correct, it should only kick the player that touched it?

No, if any other player touches it, .Touched will also fire, which means everyone will be kicked whenever 1 person touches it. You will need to add a check like what I did in the script I replied with.

Ah, so this script should only click the player that touched the part?

local TweenService = game:GetService("TweenService")
local FadeFrame = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("FadeGui").FadeFrame
local LocalPlayer = game:GetService("Players").LocalPlayer

local Part = game.Workspace.TeleportKick

Part.Touched:Connect(function(hit)
	if hit.Parent ~= LocalPlayer.Character then
		return
	end

	local fadeInTween = TweenService:Create(FadeFrame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {BackgroundTransparency = 0})
	fadeInTween:Play()
	fadeInTween.Completed:Wait(1.5)

	LocalPlayer:Kick("An unknown error occured.")
end)

Yes, also, adding a number inside Completed:Wait() won’t do anything as far as I know. You will have to add another task.wait(n) after that :Wait() to wait longer.

1 Like

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