I made this trail script, but for some reason if a player dies, it does not reload. Can anyone help me out please?
local char = game.Players.LocalPlayer.Character local trail = game.Lighting.Trail:Clone() local mouse = game.Players.LocalPlayer:GetMouse() trail.Parent = char.Head local attachment0 = Instance.new("Attachment",char.Head) attachment0.Name = "TrailAttachment0" local attachment1 = Instance.new("Attachment",char.Torso) attachment1.Name = "TrailAttachment1" trail.Attachment0 = attachment0 trail.Attachment1 = attachment1 mouse.KeyUp:connect(function(KeyUp) if KeyUp == '0' then trail.Enabled = false end end) mouse.KeyDown:connect(function(KeyDown) if KeyDown == '0' then trail.Enabled = true end end)
Trail works perfectly if the player does not die. BTW It is located in lighting.
This wiki should help!
http://wiki.roblox.com/index.php?title=API:Class/Humanoid/Died
just replace the print with the code that gives the player a trail!
Use this
local trail = nil game.Players.LocalPlayer.CharacterAdded:connect(function(char) trail = game.Lighting.Trail:Clone() local mouse = game.Players.LocalPlayer:GetMouse() trail.Parent = char.Head local attachment0 = Instance.new("Attachment",char.Head) attachment0.Name = "TrailAttachment0" local attachment1 = Instance.new("Attachment",char.Torso) attachment1.Name = "TrailAttachment1" trail.Attachment0 = attachment0 trail.Attachment1 = attachment1 end) mouse.KeyUp:connect(function(KeyUp) if KeyUp == '0' then If trail then trail.Enabled = false end end end) mouse.KeyDown:connect(function(KeyDown) if KeyDown == '0' then if trail then trail.Enabled = true end end end)
There are several problems with your script. Those are:
KeyDown
and KeyUp
are both deprecated, so you have to use UserInputService instead.
connect
is also deprecated. The new method is Connect
The Lighting is not a place for storage, you are supposed to use ReplicatedStorage and ServerStorage
And the most important: Other players will not see the trail if FilteringEnabled is on. so you would use remote events and functions.
But the one causing your issue is that you have to connect the function to CharacterAdded if you want it to work if they respawn.
Solution:
Put the Trail inside ServerStorage
Put a RemoteFunction called CreateTrail in the ReplicatedStorage
Put a RemoteEvent called EnableTrail in the ReplicatedStorage
LocalScript
local player = game.Players.LocalPlayer local Replicated = game:GetService("ReplicatedStorage") local CreateTrail = Replicated:WaitForChild("CreateTrail") local EnableTrail = Replicated:WaitForChild("EnableTrail") local UserInputService = game:GetService("UserInputService") local trail player.CharacterAdded:Connect(function(char) trail = CreateTrail:InvokeServer(char:WaitForChild("Head"),char:WaitForChild("Torso")) end) UserInputService.InputBegan:Connect(function(input,processed) if trail == nil then return end if processed then return end if input.KeyCode == Enum.KeyCode.Zero then EnableTrail:FireServer(trail,true) end end) UserInputService.InputEnded:Connect(function(input,processed) if trail == nil then return end if processed then return end if input.KeyCode == Enum.KeyCode.Zero then EnableTrail:FireServer(trail,false) end end)
Script in ServerScriptService
local Replicated = game:GetService("ReplicatedStorage") local ServerStorage = game:GetService("ServerStorage") local Trail = ServerStorage:WaitForChild("Trail") local CreateTrail = Replicated:WaitForChild("CreateTrail") local EnableTrail = Replicated:WaitForChild("EnableTrail") CreateTrail.OnServerInvoke = function(player,p1,p2) local new = Trail:Clone() local a1 = Instance.new("Attachment") a1.Name = "Attachment1" a1.Parent = p1 local a2 = Instance.new("Attachment") a2.Name = "Attachment2" a2.Parent = p2 new.Attachment0 = a1 new.Attachment1 = a2 new.Enabled = false new.Parent = p1 return new end EnableTrail.OnServerEvent:Connect(function(player,trail,enable) if trail:IsA("Trail") then trail.Enabled = enable end end)