Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

FindFirstChild() results with an error but no bug?

I have a game that uses FindFirstChild() to give the player money when they touch a brick. It results with an error but still works. My code is something like this (not exact cause I am on mobile and doing it off my head):

local denounce = 0

script.Parent.Touched:connect(function(player)
	If debounce == 0 then
		debounce = 1
		local plr = player:GetPlayerFromCharacter()
		local stats = plr:FindFirstChild('leaderstats')
		local value = stats:FindFirstChild('Money')
		value.Value = value.Value + 100
		wait(5)
		debounce = 0
end

Thanks, JailBreaker_13

2 comments

0

Why did you close my question for not being constructive? Everyone has their own issues and problems and sometimes they don't know where to start. You blatantly removed my question for "not being constructive enough" which left me with a negative 5 reputation. Everyone starts from somewhere and YOU are not acknowledging that.

0

I'm sorry if you are upset, but I was just doing my job, thank you for your feedback, I will take this into consideration when moderating. It was not just me who thought it was not constructive, there are many others that voted on it as well.

1 answer

Hello. There are quite some problems and recommendations I will give you with your script.


Problems:

  • You did not use the argument of GetPlayerFromCharacter().

  • You forgot an end.

  • You did not check if the part hit was a player.


Solutions:

  • Make the argument of GetPlayerFromCharacter() "hit.Parent".

  • Add an end.

  • Make an if statement checking if the player is what touched the part.


Recommendations:

  • Use connect with an uppercase "C" as connect is deprecated. It is never a good idea to use deprecated functions.

  • Rename the parameter for the touched event to "hit" as it makes more sense.

  • You don't need to use FindFirstChild() since all players will have a leaderstats and money value.

  • Use a true and false based debounce instead of a number based one.


Fixed Script:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	local plr = player:GetPlayerFromCharacter(hit.Parent)

	if not debounce and plr then
		debounce = true
		local stats = plr.leaderstats
		local value = stats.Money
		value.Value = value.Value + 100
		wait(5)
		debounce = false
	end
end

3 comments

0

Thanks for your help. (Some errors were because i am on mobile.)

0

Alright and no problem.

0

This did not work.

This discussion is preserved as it was. New answers, comments, and votes are closed.