Trail only shows on you after you die, help?

Alright so I have a shop, and I’m trying to make trail cosmetic. It works, and you can keep it however it doesn’t apply as soon as you buy it, it waits until you die, then every time after that it loads. If you leave and rejoin sometime later, it doesn’t show unless you diie and respawn. How can I fix this issue? Here is the code that handles trails (Server Script):

local DSS = game:GetService("DataStoreService")
local rtstore = DSS:GetDataStore("RTStore")
local price = 675

game.Players.PlayerAdded:Connect(function(player)
	local function trail()
		player.CharacterAdded:Connect(function(Character)
			local torso = Character.Torso
			local A0 = Instance.new('Attachment')
			local A1 = Instance.new('Attachment')
			A0.Position = Vector3.new(0, 1, -0.5)
			A1.Position = Vector3.new(0, -1, -0.5)
			local Trail = game.ServerStorage.RainbowTrail:Clone()
			Trail.Attachment0 = A0
			Trail.Attachment1 = A1
			A0.Parent = torso
			A1.Parent = torso
			Trail.Parent = torso
		end)
	end
	local Data = rtstore:GetAsync(player.UserId)
	if Data == true then
		trail()
	else
		game.ReplicatedStorage.BuyRainbowTrail.OnServerEvent:Connect(function(player)
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Coins.Value -= price
				trail()
				rtstore:SetAsync(player.UserId, true)
			end
		end)
	end
end)

Any help please?

put the local function trail() inside characteradded event

does it matter if it throws the warning ‘unknown global “trail”’? if i try removing the ‘local’ part from it, it tells me to consider changing it to local.

local DSS = game:GetService("DataStoreService")
local rtstore = DSS:GetDataStore("RTStore")
local price = 675

game.Players.PlayerAdded:Connect(function(player)
	local function trail()
		local character = player.Character or player.CharacterAdded:Wait()
			local torso = Character.Torso
			local A0 = Instance.new('Attachment')
			local A1 = Instance.new('Attachment')
			A0.Position = Vector3.new(0, 1, -0.5)
			A1.Position = Vector3.new(0, -1, -0.5)
			local Trail = game.ServerStorage.RainbowTrail:Clone()
			Trail.Attachment0 = A0
			Trail.Attachment1 = A1
			A0.Parent = torso
			A1.Parent = torso
			Trail.Parent = torso
		end)
	end
	local Data = rtstore:GetAsync(player.UserId)
	if Data == true then
		trail()
	else
		game.ReplicatedStorage.BuyRainbowTrail.OnServerEvent:Connect(function(player)
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Coins.Value -= price
				trail()
				rtstore:SetAsync(player.UserId, true)
			end
		end)
	end
end)

not really sure what happened however if you have trail on and you die the game doesn’t give you the trail anymore, here’s what have (it’s a little edit of yours)

local DSS = game:GetService("DataStoreService")
local rtstore = DSS:GetDataStore("RTStore")
local price = 675

game.Players.PlayerAdded:Connect(function(player)
	local function trail()
		local Character = player.Character or player.CharacterAdded:Wait()
		local torso = Character.Torso
		local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Position = Vector3.new(0, 1, -0.5)
		A1.Position = Vector3.new(0, -1, -0.5)
		local Trail = game.ServerStorage.RainbowTrail:Clone()
		Trail.Attachment0 = A0
		Trail.Attachment1 = A1
		A0.Parent = torso
		A1.Parent = torso
		Trail.Parent = torso
	end
	local Data = rtstore:GetAsync(player.UserId)
	if Data == true then
		trail()
	else
		game.ReplicatedStorage.BuyRainbowTrail.OnServerEvent:Connect(function(player)
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Coins.Value -= price
				trail()
				rtstore:SetAsync(player.UserId, true)
			end
		end)
	end
end)

oh nvm
try this

local DSS = game:GetService("DataStoreService")
local rtstore = DSS:GetDataStore("RTStore")
local price = 675

function trail(char)
			local torso = Character.Torso
			local A0 = Instance.new('Attachment')
			local A1 = Instance.new('Attachment')
			A0.Position = Vector3.new(0, 1, -0.5)
			A1.Position = Vector3.new(0, -1, -0.5)
			local Trail = game.ServerStorage.RainbowTrail:Clone()
			Trail.Attachment0 = A0
			Trail.Attachment1 = A1
			A0.Parent = torso
			A1.Parent = torso
			Trail.Parent = torso
end

game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(Character)
        trail(Character)
		end)

	local Data = rtstore:GetAsync(player.UserId)
	if Data == true then
    trail(player.Character)
	else
		game.ReplicatedStorage.BuyRainbowTrail.OnServerEvent:Connect(function(player)
			if player.leaderstats.Coins.Value >= price then
				player.leaderstats.Coins.Value -= price
				trail(player.Character)
				rtstore:SetAsync(player.UserId, true)
			end
		end)
	end
end)

game doesn’t recognize 'character, here is the error:
BuyRainbowTrail:6: attempt to index nil with ‘Torso’

oopsie, i updated the code, try again.

hey did you mean to add a param?

function trail(char)
	local torso = char.Torso
    ...
end

it did work however, thanks so much.

1 Like

oh yeah, forgot to put that one! and np :smiley: