Make player Invisible and visible back

So I saw this post and try the code and it work

but I want to make have a duration
like this bar :
image
when it runs out like this :
image

the player will be back visible
here’s the full code :
Server :

local tool = script.Parent

local RemoteFunction = Instance.new("RemoteFunction", game.ReplicatedStorage.Remotes)
RemoteFunction.Name = "ChangeVisibility"
local LastInvoke = {}
local Cooldown = 5
local CooldownForTrueOnly = true

RemoteFunction.OnServerInvoke = function(player, value)
	local Found = nil
	for i,v in pairs(LastInvoke) do
		if v[1] == player.UserId then
			Found = v
		end
	end
	if Found then
		if CooldownForTrueOnly then
			if value then
				if tick() - Found[2] >= Cooldown then
					for i,v in pairs(LastInvoke) do
						if v == Found then
							v[2] = tick()
						end
					end
				else
					print("Visiblity Cooldown For: "..player.Name..", Time Left: "..math.ceil(Cooldown - (tick() - Found[2])).."s")
					return {false, Cooldown - (tick() - Found[2])}
				end
			end
		else
			if tick() - Found[2] >= Cooldown then
				for i,v in pairs(LastInvoke) do
					if v == Found then
						v[2] = tick()
					end
				end
			else
				print("Visiblity Cooldown For: "..player.Name..", Time Left: "..math.ceil(Cooldown - (tick() - Found[2])).."s")
				return {false, Cooldown - (tick() - Found[2])}
			end
		end

	else
		table.insert(LastInvoke, {player.UserId, tick()})
	end
	local Character = player.Character or player.CharacterAdded:Wait()
	if Character then
		for i,v in pairs(Character:GetChildren()) do

			if v:IsA("Accessory") then
				if value then v.Handle.Transparency = 1 else v.Handle.Transparency = 0 end
			else
				if v.Name ~= "HumanoidRootPart" then
					pcall(function()
						if value then v.Transparency = 1 else v.Transparency = 0 end
					end)
				end
			end
			if v.Name == "Head" then
				if value then v.face.Transparency = 1 else v.face.Transparency = 0 end
			end
			if v:IsA("Tool") then
				if value then tool.Handle.Transparency = 1 else tool.Handle.Transparency = 0 end
			end
		end
	end
	return true
end

Client :

local enabled = false
script.Parent.Name = "Invisibility: Off"


script.Parent.Activated:Connect(function()	
	if enabled == false then
		local result = game.ReplicatedStorage.Remotes:WaitForChild("ChangeVisibility"):InvokeServer(true)
		if result == true then
			game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
			enabled = true
			script.Parent.Name = "Invisibility: On"
		elseif type(result) == "table" then
			game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
			script.Parent.Name = "Cooldown"
			wait(result[2])
			script.Parent.Name = "Invisibility: Off"
		end
	elseif enabled == true then
		local result = game.ReplicatedStorage.Remotes:WaitForChild("ChangeVisibility"):InvokeServer(false)
		if result == true then
			game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
			enabled = false
			script.Parent.Name = "Invisibility: Off"
		end
	end
end)

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
	enabled = false
	script.Parent.Name = "Invisibility: Off"
end)

if you guys have any idea how to do it please reply, thank you!

Something that could help you is a video of an invisibility power I made for one of my how to make a super power training simulator like game tutorial.

1 Like

for the server:
put 3rd value into the LastInvoke table as the value of whether they last went invisible or visible, this can be used in a while loop to check if they’ve been visible for the max amount of time and you can set them back to visible & reset the tick & 3rd
table.insert(LastInvoke, {player.UserId, tick(), value})

for the client (something like this):

local Stop = false
local firstClick = true
Tool.Activated:Connect(function()
    if Stop then return end
    if firstClick then
        firstClick = false
        InvokeServer(true)
        local MaxDuration = 6
        local StartTick = tick()
        repeat
            wait()
            local SecondsLeft = MaxDuration - math.ceil(tick() - StartTick)
            if SecondsLeft <= 0 then
                break
            else
                GuiFrame.Size = UDim2.fromScale(SecondsLeft/MaxDuration, 1)
            end
        until
             Stop
        InvokeServer(false)
        Stop = false
        firstClick = true
    else
        Stop = true
    end
end)
1 Like

it works! thanks for the help :smiley: