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)
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
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).
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 ).
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