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

Script not giving the player the Rainbow Trail even though they own the gamepass. Any help here?

Server Script in ServerScriptService:

local ServerStorage = game.ServerStorage.RainbowTrail:Clone()
local gamepassid = 10498381
game.Players.PlayerAdded:Connect(function(plr)
local gamepassOwned = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, gamepassid)
plr.CharacterAdded:Connect(function(char)
      if gamepassOwned then
      game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		ServerStorage.Parent = char.Head
		local attachment0 = Instance.new("Attachment",char.Head)
		attachment0.Name = "TrailAttachment0"
		local attachment1 = Instance.new("Attachment",char.HumanoidRootPart)
		attachment1.Name = "TrailAttachment1"
		ServerStorage.Attachment0 = attachment0
		ServerStorage.Attachment1 = attachment1
	end)
end)
end
end)
end)

This script worked before I added the gamepass needing to be required.

Trail in ServerStorage: https://prnt.sc/tjsdfc

1 answer

hey so, let's note off a few things: UserOwnsGamePassAsync Yields. that means the code after it will have to wait until the function is finished for it to run, that's not good for our code, thankfully we can just call it multiple times because the result of the function is cached after its first use, so we won't be making a request each time.

CODE WITHOUT COMMENTS HERE

local ServerStorage = game.ServerStorage.RainbowTrail:Clone()
local gamepassid = 10498381

local function hasGP(pid, gpid)
	return game:GetService("MarketplaceService"):UserOwnsGamePassAsync(pid, gpid) -- return true or false depending on if the player has the gamepass or not.
end

local function attach(child, attachments)
-- child is the trail
-- attachments are the parts it's going to use as attachments, categorized in a table
	for i = 1,#attachments do  -- loop through the table
		local offset = tostring(i -1) -- create the offset between the for loop and the integers needed to set the attachments
		local attachment = Instance.new("Attachment") -- create the attachment
		attachment.Parent = attachments[i] -- parent it to the corresponding part
		attachment.Name = "TrailAttachment"..offset -- name it according to its Attachment value.
		child["Attachment"..offset] = attachment -- set the trail's Attachment to the one we just created.
	end
	child.Parent = attachments[1] -- set the trail's parent to the first attachment part, in this case the character's head, which is what as in your code, feel free to change this.
end

game.Players.PlayerAdded:Connect(function(plr) -- connect to PlayerAdded
	plr.CharacterAdded:Connect(function(chr) -- Connect to CharacterAdded
		if hasGP(plr.UserId, gamepassid) then -- Check for gamepass
			attach(ServerStorage, {chr.Head, chr.HumanoidRootPart}) -- Create attachment
		end
	end)
end)

The code above was fully tested by me and I confirm it works as intended.

3 comments

0

Thank you so much!

0

For some reason the trail only works on 1 player at a time. Ex: I had the trail, my friend joined, my trail got deleted and the trail went to him.

0

Wow, I'm late sorry. To fix that simply remove ":Clone()" From ServerStorage and add it to the "attach" function. attach(ServerStorage:Clone(), {chr.Head, chr.HumanoidRootPart}). Sorry for the mistake.

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