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

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

I’m not sure what your trying to do my script should of 100% worked. Put a script INSIDE the part you want touched then copy and paste what I put.

I want it so if you know a tycoon and if a part touches the end of the conveyer then it destroys it maybe give a leaderstats i have one but that might be the problem

The part does not have a humanoid im making a tycoon a tycoon is a basically spell up tycoon and play one idk how to define it

Ok i now know how to define tycoon a tycoon is a business sorry if i spelt that wrong but its a business and they make lots of money and they go higher and higher, its kind of a simulator

Im also just gonna try and just put them in diffrent scripts one for destroying and one for token giving