Proximityprompt to character fade

Hi i want to make a script that, when the proximity prompt is triggered, it’ll fire an event and make the character fade.
But it looks like it isn’t working and there isn’t any errors showing up in the output.

localscript:

local rs = game:GetService("ReplicatedStorage")
local remoteEvent = rs:WaitForChild("Character"):WaitForChild("characterTeleport")

local proximityPrompt = game.Workspace:WaitForChild("book"):WaitForChild("Plane"):WaitForChild("Attachment"):WaitForChild("ProximityPrompt")

local debounce = false

proximityPrompt.Triggered:Connect(function()
	if debounce == false then
		debounce = true
		remoteEvent:FireServer()
		debounce = false
	end
end)

script

local rs = game:GetService("ReplicatedStorage")
local remoteEvent = rs:WaitForChild("Character"):WaitForChild("characterTeleport")

local debounce = false

function fade(v)
	for i = 1,10 do
		v.Transparency = v.Transparency + 0.1
		wait()
	end
end

remoteEvent.OnServerEvent:Connect(function()
	game.Players.PlayerAdded:Connect(function(player)
		player.Character:Connect(function(character)
			for i, v in pairs(character:GetChildren()) do
				if v.ClassName == "MeshPart" or v.ClassName == "Part" then
					fade(v)
				end
			end
		end)
	end)
end)

Thanks for helping me out

OnServerEvent returns the player who fire it, you can use it

local rs = game:GetService("ReplicatedStorage")
local remoteEvent = rs:WaitForChild("Character"):WaitForChild("characterTeleport")

local debounce = false

function fade(v)
	for i = 1,10 do
		v.Transparency += 0.1
		task.wait()
	end
end

remoteEvent.OnServerEvent:Connect(function(Player)
	-- Verify that the player has an associated character
	local character = Player.Character
	if not character then		return		end

	for i, v in pairs(character:GetChildren()) do
		if not v:IsA("BasePart") then		continue		end
		fade(v)
	end
end)

in case the parts must be all at the same time

local rs = game:GetService("ReplicatedStorage")
local remoteEvent = rs:WaitForChild("Character"):WaitForChild("characterTeleport")

local debounce = false

function fade(v)
	for i = 1,10 do
		v.Transparency += 0.1
		task.wait()
	end
end

remoteEvent.OnServerEvent:Connect(function(Player)
	local character = Player.Character
	if not character then		return		end
	for i, v in pairs(character:GetChildren()) do
		if not v:IsA("BasePart") then		continue		end
		task.spawn(fade, v)
	end
end)

if it must take up the accessory handle, replace GetChildren with GetDescendants.


Source: task.spawn and task.wait

Successfully worked thank you!!

Is there any way to also fade the accessories?

Yes, as I put at the end of the post, you can use GetDescendants instead of GetChildren so that it fades them too.

1 Like

Also, you do not have to use a RemoteEvent as ProximityPrompt.Triggered fires on the server too.

2 Likes

As has been suggested because “ProximityPrompt” instances can be triggered on the server the RemoteEvent instance isn’t required. When the “.Triggered” event is fired through/on a prompt the player instance belonging to the client which triggered it is automatically passed as a an argument to any callback function connected to the event via the connection instance method “:Connect()”.

Instead of a loop you could also consider using tweens for a more seamless fading animation. The following is a rewrite of your script with the inclusion of tweens. This implementation only requires a single server script inside a proximity prompt instance.

local Tweens = game:GetService("TweenService")
local Prompt = script.Parent

local Debounce = false

local function CreateTween(Object)
	local Tween = Tweens:Create(Object, TweenInfo.new(1), {Transparency = 1})
	Tween:Play()
end

local function FadeOutRecursive(Object)
	for _, Child in ipairs(Object:GetChildren()) do
		if Child:IsA("BasePart") then
			CreateTween(Child)
			FadeOutRecursive(Child)
		elseif Child:IsA("Decal") then
			CreateTween(Child)
		elseif Child:IsA("Accessory") then
			FadeOutRecursive(Child)
		end
	end
end

local function OnPromptTriggered(Player)
	if Debounce then
		return
	end
	Debounce = true

	local Character = Player.Character
	FadeOutRecursive(Character)
	task.wait(1)
	Debounce = false
end

Prompt.Triggered:Connect(OnPromptTriggered)
1 Like