前面写了一个触碰隐身功能,但是那个方式不太好,今天换一个方式
首先创建一个 TextButton ,为他添加一个localscript,以及在ReplicatedStorage下添加一个RemoteEvent用来事件通信
local On = script.Parent
local remoteEvent = game.ReplicatedStorage["RemoteEvent"]
local CD_Time=5
function CD()
for i=5,0,-1 do
On.Text = CD_Time
wait(1)
CD_Time = CD_Time-1
end
if On.Text == tostring(0) then
On.Text='隐身'
CD_Time=5 --重置CD时间
end
end
On.MouseButton1Click:Connect(function()
if On.Text == "隐身" then
remoteEvent:FireServer(On.Text) -- 向服务器发送消息
On.Text = "现身"
wait(5)--5秒后技能消失,自动现身
remoteEvent:FireServer(On.Text)
elseif On.Text == "现身" then
remoteEvent:FireServer(On.Text) -- 向服务器发送消息
CD()
end
end)
接下来是服务器脚本
local remoteEvent = game.ReplicatedStorage["RemoteEvent"]
local Table={} --用于存储实体
function Add(player,Text)
if Text=='隐身' then
local Child = player.Character:GetDescendants()
local Sj = {}
for k,v in pairs(Child) do
if v.ClassName=='MeshPart' then
v.Transparency = 1
elseif v.ClassName=="Accessory" then
table.insert(Sj,v)
v.Parent = game.ServerStorage
elseif v.ClassName=='Decal' then
v.Parent = game.ServerStorage
table.insert(Sj,v)
end
end
Table[tostring(player.UserId)]=Sj
else
local Character =player.Character
for k,v in pairs(Table[tostring(player.UserId)]) do
if v.Name=='face' then
local Head = Character:FindFirstChild("Head")
v.Parent = Head
else
v.Parent = Character
end
local Child = Character:GetDescendants( )
for k,v in pairs(Child) do
if v.ClassName=='MeshPart' then
v.Transparency = 0
end
end
end
Table[player.Name]=nil --移除数据
end
end
remoteEvent.OnServerEvent:Connect(Add)
大功告成