I’m literally just trying to use a basic .Changed function on an int value I created earlier in the script and put in a folder in the player. When I try to do that I get this error - [ attempt to index nil with ‘Changed’] I know this means that the value isn’t there, but the problem is it is!
Basically the utility easily creates values without extra steps, but while this works perfectly for leaderstats, for this folder it does nothing. Also in my other script where I try to get it from the player inside a playeradded function it says infinite yield possible on the folder.
function onPlayerAdded(plr)
--[[stats = plr:WaitForChild("PlayerStats")
print(stats.Name)
speed = stats:FindFirstChild("Speed")--]]
--other stuff
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
If I go into the server the folder is definitely there which confuses me.
If you need any extra information ask! Thanks.
Ok I did that, and it said a weird error about a concentrated string (that now that I changed back from .Name I can’t seem to reproduce). So i added .Name to that and then it said, attempted to index nil with parent.
Right. This means that the instance either doesn’t get created with your module, or you’re trying to access it too soon. Can you provide the utility.new() code? You might’ve forgotten to return the value after it’s created.
Ok here is a screenshot of what is inside of my player on the server (It looks the same on the client). Screenshot by Lightshot
local utility = {}
utility.new = function(a,b,c,d)
local i = Instance.new(a)
if c then
i.Name = c
end
if d then
for p,v in next,d do
i[p]=v
end
end
if b then
i.Parent = b
end
return i
end
return utility
It prints folder (leaderstats), intvalue x3 then folder again (playervalues) then intvalue x2. I’m really confused because it is showing up in the player, but it stays saying it’s nil.
same error- [1:46:13.837 - ServerScriptService.Server:246: attempt to index nil with ‘Parent’]
i then tried just printing playerstats which said attempted to index nil with Name. So is the script running like while creating the values moving on to the next part of the script? I’m going to sleep maybe I’ll figure it out in the morning thanks for trying to help.
I find this odd. When I try this for myself, it works perfectly. This is just a blind shot, but could you try adding a wait() in-between local playerStats and speed?
@spicychilly2 And this is probably the weirdest part. I reference speed in another spot,
local function onCharacterAdded(character)
local player = players:GetPlayerFromCharacter(character)
humanoid = character:FindFirstChild("Humanoid")
humanoid.WalkSpeed = speed.Value
and it works fine? I also change speeds value in multiple places and it works fine. That’s why I’m so confused about why it’s only giving nil in the .Changed. I’m going to try one more thing and that is move that function right next to where I create speed.
That’s why speed is not local when I create it so I can reference it in other places, though this might not be the best practice I’m not sure.
Ok well I moved the Changed function next to where I create speed, and now I’m no longer getting an error which is nice, but in my other scripts where I reference the folder I’m still getting and infinite yield possible on it.
Couldn’t reproduce the issue. I wrote pretty much the same code as you did except with my own conventions in place and I did not get any such error. Are you perhaps overwriting a variable or not properly calling your module? It’d be worth printing what “speed” is to further debug this.
My version of utility
local utility = {}
function utility.new(class, name, parent, propertiesTable)
local instance = Instance.new(class)
if name then
instance.Name = name
end
if propertiesTable then
for property, value in pairs(propertiesTable) do
-- Don't want it happening even accidentally
if property ~= "Parent" then
instance[property] = value
end
end
end
if parent then
instance.Parent = parent
end
return instance
end
return utility
My unit testing code
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local UTILITY = require(ServerScriptService.Utility)
local function playerAdded(player)
local playerStats = UTILITY.new("Folder", "PlayerStats", player)
local speed = UTILITY.new("IntValue", "Speed", playerStats)
speed.Changed:Connect(function (newValue)
print(newValue)
end)
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end