Int Value is not a valid member... Despite it being there

Hello! I am trying to create a level system using data stores to save values, the only issue is that for certain players it returns an error saying “XP is not a valid member of ReplicatedStorage.(player name)”

I am not sure why it’s doing this as the value is there and present, though all my attempts to retrieve the value when on an account that has this glitch returns nil.

Event a Bindable event sending the reference of the value to the script that needs it isnt working properly, and even more strangely the script that created it is only able to access it while in the function it was created, despite the parent properties being correctly set up.

Here are the scripts:

Data Store script

local dataStore = game:GetService("DataStoreService")
local level = dataStore:GetDataStore("Level")

local function getInfo(plr)
	local array
	local success, er = pcall(function()
		array = level:GetAsync(plr.UserId.."Level")
	end)
	if success then
		return array
	else
		warn(er)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local folder = Instance.new("Folder",game.ReplicatedStorage)
	folder.Name = player.Name
	local level = Instance.new("IntValue",leaderstats)
	level.Name = "Level"
	local xp = Instance.new("IntValue",folder)
	xp.Name = "XP"
	local array = getInfo(player)
	if array[1] then
		level.Value = array[1]
	else
		level.Value = 0
	end
	if array[2] then
		xp.Value = array[2]
	else
		xp.Value = 0
	end
	player.PlayerGui:WaitForChild("Level",5).GetXp:Fire(xp)
	print("FINISHED SETUP")
end)

game.Players.PlayerRemoving:Connect(function(player)
	local array = {
		[1] = player.leaderstats.Level.Value,
		[2] = game.ReplicatedStorage[player.Name].XP.Value
	}
	level:SetAsync(player.UserId.."Level",array)
	game.ReplicatedStorage[player.Name]:Destroy()
end)

Level script

local dataStoreService = game:GetService("DataStoreService")

local kdrDataStore = dataStoreService:GetDataStore("levelDataStore")

wait(2)

local tweenService = game:GetService("TweenService")

local plr = script.Parent.Parent.Parent

local values = game.ReplicatedStorage[plr.Name]
local sounds = script.Parent.Sounds

local levelUp = sounds.LevelUp
local mileLevelUp = sounds.MoreLevelUp
local duelLevelUp = sounds.duelUp
local milestone = sounds.milestone

local xpCollect = sounds.XPCollect

local frame = script.Parent.Frame

local currentXp = values:WaitForChild("XP",5)
warn(currentXp)
local myLevel = plr.leaderstats.Level

local levelBar = script.Parent.LevelBar

local levelUpText = frame.Levelup
local nowLevelText = frame.nowlevel
local milestoneText = frame.milestone

local info = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.In)

local enterLevelUp = tweenService:Create(levelUpText,info,{Position = UDim2.new(0.336, 0,0.304, 0)})
local enterCurrentLevel = tweenService:Create(nowLevelText,info,{Position = UDim2.new(0.393, 0,0.493, 0)})

local exitLevelUp = tweenService:Create(levelUpText,info,{Position = UDim2.new(-1.336, 0,0.304, 0)})
local exitCurrentLevel = tweenService:Create(nowLevelText,info,{Position = UDim2.new(1.393, 0,0.493, 0)})

local contextIndex = 1

local changing = false

local leveling = false

local milestoneArray = {
	[5] = true
}

local function levelGui()
	script.Parent.Event:FireClient(plr,contextIndex)
	print("playing tween")
	enterLevelUp:Play()
	wait(.5)
	enterCurrentLevel:Play()
	if milestoneArray[myLevel.Value] then
		contextIndex = 4
	end
	if contextIndex == 4 then
		milestoneText.TextTransparency = 1
		milestoneText.TextStrokeTransparency = 1
		milestoneText.Visible = true
		script.Parent.Event:FireClient(plr,5)
		wait(.5)
		for i = 1,0,-.05 do
			milestoneText.TextTransparency = i - .05
			milestoneText.TextStrokeTransparency = i - .05
			wait()
		end
		wait(.5)
		for i = 0,1,.05 do
			milestoneText.TextTransparency = i + .05
			milestoneText.TextStrokeTransparency = i + .05
			wait()
		end
	else
		enterCurrentLevel.Completed:Wait()
	end
	wait(3)
	exitLevelUp:Play()
	exitCurrentLevel:Play()
	milestoneText.Visible = false
	leveling = false
end

function updateLevel(level,enemy)
	if leveling then return end
	leveling = true
	if enemy then
		
	end
	nowLevelText.Text = "Now Level: " .. level
	levelGui()
end

function updateGui(current,level,enemy)
	print(current/EF(level))
	print("updated gui")
	levelBar.Size = UDim2.new(.114*(current/EF(level)),0,levelBar.Size.Y.Scale,0)
	game.ReplicatedStorage:FindFirstChild(plr.Name).XP.Value = currentXp.Value
	--game.ServerScriptService.EventsSabers.XP:Fire(currentXp.Value,plr)
end

function EF(Level)
	print(Level)
	if Level < 1 then
		return 100
	elseif Level >= 1 and Level < 2 then
		return 500
	elseif Level < 20 and Level >= 2 then
		return 1385*math.log(math.floor(Level))
	elseif Level >= 20 and Level < 50 then
		return ((math.floor(Level)%5) + math.log(math.floor(Level))+(math.floor(Level)%10)+(math.floor(Level)/30) + 50)*100
	elseif Level >= 50 and Level < 75 then
		return 6925*math.log(math.floor(Level))
	elseif Level >= 75 and Level < 100 then
		return (((math.floor(Level)%5) + math.log(math.floor(Level))+(math.floor(Level)%10)+(math.floor(Level)/30) + 50)*2)*100
	elseif Level >= 75 and Level < 100 then
		return 13850*math.log(math.floor(Level))	
	elseif Level >= 100 and Level < 125 then
		return (((math.floor(Level)%5) + math.log(math.floor(Level))+(math.floor(Level)%10)+(math.floor(Level)/30) + 50)*4)*100
	elseif Level >= 125 and Level < 150 then
		return 69250*math.log(math.floor(Level))
	else
		return 138500*math.log(math.floor(Level))
	end
end

function addExp(currentExp,added,level)
	if (currentExp.Value+added) > EF(level.Value) then
		local leftOver = (currentExp.Value+added)-EF(level.Value)
		
		level.Value += 1
		
		currentExp.Value = 0
		
		addExp(currentExp,added,level)
		
	elseif currentExp.Value+added== EF(level.Value) then
		level.Value += 1
		currentExp.Value = 0
	else
		
		currentExp.Value += added
	end
	updateGui(currentXp.Value,level.Value)
end

local enemy

game.ServerScriptService.Events.Level.Event:Connect(function(player, added,context,enemy1)
	if player.Name ~= plr.Name then return end
	contextIndex = context
	print("added XP")
	print(added)
	enemy = enemy1
	addExp(currentXp,added,myLevel)
end)

myLevel.Changed:Connect(function(value)
	if not enemy then
		updateLevel(value)
	else
		updateLevel(value,enemy)
	end
end)

script.Parent.GetXp.Event:Connect(function(xpRef)
	if not currentXp then
		currentXp = xpRef
		warn(currentXp)
	end
end)
1 Like

I think this is where it is erroring for you, you probably need to wait for it to exist

local values = game.ReplicatedStorage:WaitForChild(plr.Name)

It had no effect, it’s specifically the value itself. The strangest part is its consistent with just my player but nobody else.