How could I increase a player's leaderstat if "1" is a child of this model?

I need this script to detect when the model has a child named “1”, and if it does, increase the player’s leaderstat by 2. Currently the player’s leaderstat is not going up and tbh I have no idea why. I have moved the script around a bit but nothing works like I said.
Here is my code so far:

while true do  --this duplicates a model (ignore it)
	local original = game.Workspace.NoobStuff["Noob Printer 1"][" "]
	local copy = original:Clone()
	copy.Parent = original.Parent
	copy.Name = "1"
	copy.Head.Anchored = false
	copy.HumanoidRootPart.Anchored = false
	copy["Left Arm"].Anchored = false
	copy["Left Leg"].Anchored = false
	copy["Right Arm"].Anchored = false
	copy["Right Leg"].Anchored = false
	copy.Torso.Anchored = false
	copy.Head.CanCollide = false
	copy.HumanoidRootPart.CanCollide = false
	copy["Left Arm"].CanCollide = false
	copy["Left Leg"].CanCollide = false
	copy["Right Arm"].CanCollide = false
	copy["Right Leg"].CanCollide = false
	copy.Torso.CanCollide = false
	copy:SetPrimaryPartCFrame(CFrame.new(-201.718, 46.177, 248.476))
	
	wait(3)
end
--this part is what I need help on
workspace.NoobStuff["Noob Printer 1"]:FindFirstChild("1")(function(player)
	local give = player.leaderstats.Friends
	give.Value += 2
	end)

Pretty sure FindFirstChild can’t be connected to a function. Try this instead:

if workspace.NoobStuff[“Noob Printer”]:FindFirstChild(“1”) then
-- The rest of your code
end

i thought this is what you wanted me to use? so I tried it and got nothing

if workspace.NoobStuff["Noob Printer 1"]:FindFirstChild("1") then
	function givefriend(player)
		local give = player.leaderstats.Friends
		give.Value += 2
	end
end

You’re putting the code in the function, you’d have to call the function.

What you’re doing:

if 1 then
function myfunction()
Code
end
end

You’re putting it in the function. Remove the function “part” like this:

if too lazy to write then
local give = too lazy still
give.Value += 1
end

Basically just visualize this:

**DELETE THIS** function givefriend(player)
		**KEEP THIS** local give = player.leaderstats.Friends
		**KEEP THIS** give.Value += 2
	***DELETE THIS** end

so when i put the code in, the player.leaderstats.friends is erroring because player isn’t said as a local or function or anything, what should I do?

Could you provide the error (or rephrase what you said) so I could better understand what’s going on w the error.

For example:

attempt to index nil with ‘char limit’

there is not an error but when I type the code in, it underlines “player” in red and says that it is an unknown global

Ah I see, here you’re using a player parameter (which for future reference, FindFirstChild does NOT return the player object the only main event you’d get the player object from is the PlayerAdded event).

A little more detailed explanation of what went wrong:

Basically, you were trying to get the Player object via a connection to a FindFirstChild event (which doesn’t exist because it’s a function). So you were trying to find a model named “1”. The FindFirstChild function does return (or “gives back”) the child if it finds it; so, that means that it would return the model “1” if it found it and NOT the player object.

If this is in a server script (hopefully because if you’re saving leaderstat values it should be on the server), just use the PlayerAdded event like so:

game.Players.PlayerAdded:Connect(function(Player)
-- stuff
end

If it’s in a LocalScript, just reference the player (local Player = game.Players.LocalPlayer).

alright so you want me to to

game.Players.PlayerAdded:Connect(function(player)
	local give = player.leaderstats.Friends
	give.Value += 2
end

I am not very good at scripting so it’s a bit confusing

1 Like

Is the script a server script (like not in StarterGui or StarterPlayerScript or StarterCharacterScripts) or is it a local script?

Added a bit extra content in my previous paragraph that may or may not help you understand what went wrong (might be too confusing and if it is then I’m sorry :sweat_smile:).

it is a server script inside of a model

Alright so yeah keep what you just wrote then.

Then run it ofc to test it.

I did and nothing happened
eeeeeeeeeeeeee

Oh wow okay there is a lot to tackle (I’m slowly discovering the errors 1 by 1).

Okay, so the while true loop is constantly running (not letting all the code after it run). So, just add a task.spawn() function so everything else can run like so:

task.spawn(function()
while true do
your code
end
end)

game.Players.PlayerAdded:Connect(function(Player)
that code you wrote
end
EXTRA

I would recommend wrapping the PlayerAdded event after you finish defining your variables but yeah

Hopefully there are no other little setbacks in your og code

hey uh sorry to say this but I gotta go if I can I will respond later but most likely not, I thank you for you help and I am really sorry

1 Like

It’s alright, take your time. :slight_smile:

1 Like