Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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!

0
But if it fires when the player dies, after the player respawns, the trail will be removed again. XionGamerHD 0 — 6y
0
add Wait(6) before the code that gives the player the trail KinqAustinn 293 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)
Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
That’s if they want filtering enabled on SilentGalaxZy 53 — 6y
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. UgOsMiLy 1074 — 6y

Answer this question