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 fix instance.new not making a folder in the right place?

Hello.

I am having trouble making a trail shop for my game. The issue is that it should make a folder names "leaderstats" in the user's stuff where all the folders are but it wont, instead it makes a folder in "Players" section and i havent found a fix for it. I have tried a ton of different ways on how to fix this but yet none worked.

Here's the code Error should be in line 46

-- Variables
local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local player = game:GetService('Players').PlayerAdded:Wait()
local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest")
local Players = game:GetService("Players")

 --Functions

local function EquipTrail(plr,trail)
	local char = plr.Character
	
	if trail ~= nil and char ~= nil then
		
		if char.HumanoidRootPart:FindFirstChild("Attachment") then char.HumanoidRootPart.Attachment:Destroy() end
		if char.Head:FindFirstChild("Attachment") then char.Head.Attachment:Destroy() end
		
		if char:FindFirstChild(plr.Name.."'s Trail") then char[plr.Name.."'s Trail"]:Destroy() end
		trail.Name = plr.Name.."'s Trail"
		
		local attachment1 = Instance.new("Attachment",char:FindFirstChild("HumanoidRootPart"))
		local attachment2 = Instance.new("Attachment",char:FindFirstChild("Head"))
		
		trail.Attachment0 = attachment1
		trail.Attachment1 = attachment2
		
		trail.Parent = char
	end
end

Players.PlayerAdded:Connect(function(Player)
	local UserId = Player.UserId
	local Currency = CurrencyStore:GetAsync(UserId)

	if Currency == nil then
		Currency = 0
		CurrencyStore:SetAsync(UserId, Currency)
	end

-- Player Joins

game.player.PlayerAdded:Connect(function(player)
	
	-- Stats for the player
	
	local Leaderstats = Instance.new("Folder",game.Players.LocalPlayer)
	Leaderstats.Name = "leaderstats"
	local coins = Instance.new("IntValue", Leaderstats)
	coins.Name = "Coins"

	coins.Value = Currency

	coins.Changed:Connect(function(NewValue)
			CurrencyStore:SetAsync(UserId, NewValue)
		end)
	end)
	
	local trailInventory = Instance.new("Folder",player); trailInventory.Name = "TrailInventory"
	local equippedTrail = Instance.new("StringValue",player); equippedTrail.Name = "EquippedTrail"
	
	-- Player's character added to the game
	
	player.CharacterAdded:Connect(function(char)
	
		if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
			EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
		end
	end)
	
	-- If the equipped Value changed, it will get their trail
	equippedTrail.Changed:Connect(function()
		if equippedTrail.Value ~= nil then
			if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
				EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
			end
		end
		if equippedTrail.Value == "" then
			
			local char = player.Character
			
			local root = char:FindFirstChild("HumanoidRootPart")
			local head = char:FindFirstChild("Head")
			
			if root:FindFirstChild("Attachment") then root.Attachment:Destroy() end
			if head:FindFirstChild("Attachment") then head.Attachment:Destroy() end

			if char:FindFirstChild(player.Name.."'s Trail") then char[player.Name.."'s Trail"]:Destroy() end
		end
	end)
	
	-- loaded up the data

	local data

	local success, errorMsg = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)

	if data ~= nil then
		if data.Coins then
			coins.Value = data.Coins
		end
		if data.EquippedTrail then
			equippedTrail.Value = data.EquippedTrail
		end
		if data.Trails then
			for index,trail in pairs(data.Trails) do
				local bool = Instance.new("BoolValue",trailInventory); bool.Name = trail
			end
		end
	else
		-- New player
		print("A new challenger approch")
	end

	-- This event will fire the client to update the gui
	game.ReplicatedStorage.Events.SendData:FireClient(player,data)
end)

game.Players.PlayerRemoving:Connect(function(player)
	-- Data holder
	local data = {}
	
	data.Coins = player.Coins.Value
	
	data.EquippedTrail = player.EquippedTrail.Value
	
	data.Trails = {}
	
	for i,v in pairs(player.TrailInventory:GetChildren()) do
		table.insert(data.Trails,v.Name)
	end
	
	local success, errorMsg = pcall(function()
		dataStore:SetAsync(player.UserId,data)
	end)
	
	-- if its succeed then the data is saved else we got an error
	
	if success then 
		print("Succesfully saved") 
	else 
		print(errorMsg) 
	end
	
end)

game:BindToClose(function()
	for index, player in pairs(game.Players:GetPlayers()) do
		local data = {}

		data.Coins = player.Coins.Value

		data.EquippedTrail = player.EquippedTrail.Value

		data.Trails = {}

		for i,v in pairs(player.TrailInventory:GetChildren()) do
			table.insert(data.Trails,v.Name)
		end

		local success, errorMsg = pcall(function()
			dataStore:SetAsync(player.UserId,data)
		end)

		-- if its succeed then the data is saved else we got an error

		if success then 
			print("Succesfully saved") 
		else 
			print(errorMsg) 
		end
	end
end)

-- Server Events

game.ReplicatedStorage.Events.BuyItem.OnServerInvoke = function(player, trailName)

	local trail = game.ReplicatedStorage.Trails:FindFirstChild(trailName)
	local ifsInInventory

	if player.TrailInventory:FindFirstChild(trailName) then
		ifsInInventory = true
	end

	if trail then
		if trail:FindFirstChild("Price") then
			if not ifsInInventory then
				-- Check if we can buy the trail
				if player.Coins.Value >= trail.Price.Value then
					print(player.Name.." brought the ".. trail.Name.." trail")

					player.Coins.Value = player.Coins.Value - trail.Price.Value

					local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = trail.Name

					return "Brought"
				else
					return "NotEnough"
				end
			else
				-- You already owned it
				if player.EquippedTrail.Value ~= trail.Name then
					player.EquippedTrail.Value = trail.Name
					print(player.Name.." equipped the ".. trail.Name.." trail")
					return "Equip"
				else
					player.EquippedTrail.Value = ""
					print(player.Name.." unequipped the ".. trail.Name.." trail")
					return "Unequip"
				end
			end
		end
	end
	end

if you can find any other errors aswell, please tell me about it. Thanks!

2 comments

0

Hey, is this a local script or a normal script? I think I may know the problem.

0

You cant declare localplayer in serverscript

3 answers

you need a parent to be listed so for example

game.Players.PlayerAdded:Connect(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Cash = Instance.new("IntValue")
Cash.Name = "Cash" --replace Cash to the currency
Cash.Parent = leaderstats
end

4 comments

0

now no folder is made or i just understood something wrong.

0

This is the correct way on how you parent something to the player. You must've typed something wrong, or you didn't use it correctly.

0

game.Players.PlayerAdded:Connect(player) local Leaderstats = Instance.new("Folder") Leaderstats.Name = "leaderstats" Leaderstats.Parent = player local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Value = Currency coins.Parent = Leaderstats coins.Changed:Connect(function(NewValue) CurrencyStore:SetAsync(UserId, NewValue) end)

0

dude you were close, it should've been game.Players.PlayerAdded:Connect(function(player), you forgot function

LocalPlayer doesn't work for server scripts. Use this:

--Not just this add the other stuff btw.
game.Players.PlayerAdded:Connect(function(player)
	local Leaderstats = Instance.new("Folder",player)
end)

From line 42 to line 49, change it to this:

game.Players.PlayerAdded:Connect(function(player)
	local Leaderstats = Instance.new("Folder",player)
	Leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",Leaderstats)
	coins.Name = "Coins"
--everything else should just be the same

15 comments

0

it still doesnt make a folder at all

0

do you get any errors in the developer console?

0

wait thats not the issue, in line 101 when you go up and see the original source code it says that that "coins" is an unknown global.

0

i think you should get rid of line 4 in your original script.

0

the script doesn't know if it should use the PlayerAdded parameter, or the variable in line 4.

0

it causes inconsistencies if you have 2 different variables with same name referring to different things

0

thats not the issue, in line 101 in the original script it gives me this error thing: Unknown global "coins"

0

and it gives me the error "leaderstats is not a valid member of Players.Boat_je but instead of giving me it 1 time it starts spamming it crazy fast lagging out the game

0

change coins in line 101 to player:WaitForChild("leaderstats"):WaitForChild("Coins")

0

because coins is instance.new IntValue, it creates a new intvalue everytime

0

i did that but it still spams "leaderstats is not a valid member of Players.boat_je" and it still didnt make any folder there

0

that's odd, that wouldn't normally happen

0

now I'm a bit clueless. I'll try to solve your question when i can use studio on my pc. I'm on my laptop rn so

0

ok, can you add me on discord so itll be easier to speak? my name and tag is TheGuy#5378

0

aight

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