Infinite yield possible on 'Game:WaitForChild("leaderstats")'

So im now making a fnaf security breach tycoon and i cant figure out the way to make it so when a part hits the destroy thing it gives you tokens

local Freddy = game.Workspace:GetChildren("Freddy")
local part = script.Parent
local tokens = game:WaitForChild("leaderstats"):WaitForChild("Tokens")

part.Touched:Connect(function()
	tokens.Value += 2
	Freddy:Destroy(Freddy)
end)

Thanks for helping me
ive ran into this multiple times i cant figure this out though

1 Like

Hmm maybe try:

local tokens = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Tokens")

Also I think Freddy:Destroy(Freddy) will error. Change it to Freddy:Destroy(). If it still errors, then you have to change your local Freddy variable to this:

local Freddy = game.Workspace:WaitForChild("Freddy")
1 Like

It didnt work i think the reason is the token thing

1 Like

Do you need both fixed or is it the block not breaking?

1 Like

Both of them the block breaking was fine

1 Like

What I do to fix this is wait a few seconds to load the script, here we go.

task.wait(5)
local Freddy = game.Workspace:GetChildren("Freddy")
local part = script.Parent
local tokens = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Tokens")

part.Touched:Connect(function()
	tokens.Value += 2
	Freddy:Destroy(Freddy)
end)

Script wont work until 5 seconds has passed it gives it time so it lets roblox load everything in

I saw you did “game:WaitForChild(“leaderstats”)” You need to get the player. There is a way to do what you want to do on the client BUT if you add datastore it wont save, you need to use remote event or remote function to connect to the server AND a better touched detection. You need to look over the devforum to use Remotes and a better touch detections.

1 Like

Is the code, that you have given, is in a Local Script or a Server Script?

1 Like

In a dropper and its just a script

Okay. The reason you are getting this error is because “leaderstats” does not exist when you just call game, it is in the player object. Basically, you just want to get the player from the touch event, and then get the leaderstats folder that is in the player object and increase the specific value in leaderstats folder.

Use the following code below:

local Freddy = game.Workspace:WaitForChild("Freddy")
local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- if whatever touched the part has a humanoid
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- gets the player from the character

		if player then -- double checks if the player exist
			player:FindFirstChild("leaderstats").Tokens += 2 -- adds the value
			Freddy:Destroy() --destroys that
		end
	end
end)

For Server side you can do smth like this

local part = script.Parent

Part.Touched:Connect(function(Hit)
      if Hit.Parent ~= nil then
          local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

            if Player then
                  local leaderstats = Player:WaitForChild("leaderstats")
                  local Tokens = leaderstats:WaitForChild("Tokens")

                 Tokens.Value += 2
            end
      end
end)

I shouldn’t of really gave u the full solution but you would of never of got all of this if your a starter, just put this in a server script and it should fix your issue, I havent tested so if anything doesnt work please let me know

Ok uhh, That didnt work and i dont know if your looking at what im looking at i need it for a tycoon and im thinking that i should put them in two diffrent scripts

And it now also just says this with all the scripts

Did you try the code that I gave and the code that @DevFoyRBX ? Are there any errors in the output? If so, what is the error?

That and its with every script

Wait, this error does not seem relevant to the code that I have given. There is no :WaitForChild() on the 4th line of the code I gave above. Who’s code have you used right now?

The first one by DevFoyRBX and ill try others but i think there all the same

Might take a little while though

@workingongamessoon Use the code I have given above

This is not the same code.

Ok it might work, the problem is, that it wont find the part first when it spawns yes it spawns basically read about it here

K i gtg to bed ill finish tomorrow