Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make my Trail script reload each time the player dies?

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.

3 answers

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:

  1. KeyDown and KeyUp are both deprecated, so you have to use UserInputService instead.

  2. connect is also deprecated. The new method is Connect

  3. The Lighting is not a place for storage, you are supposed to use ReplicatedStorage and ServerStorage

  4. And the most important: Other players will not see the trail if FilteringEnabled is on. so you would use remote events and functions.

  5. 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)

2 comments

0

That’s if they want filtering enabled on

0

But if Filtering Enabled is off, your game won't be visible to accounts under 13, so it will have a little chance of people playing it. Plus, FilteringEnabled makes your game more secure.

This discussion is preserved as it was. New answers, comments, and votes are closed.