Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

FindFirstChild() results with an error but no bug?

Asked by 4 years ago
Edited by youtubemasterWOW 4 years ago

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

01local denounce = 0
02 
03script.Parent.Touched:connect(function(player)
04    If debounce == 0 then
05        debounce = 1
06        local plr = player:GetPlayerFromCharacter()
07        local stats = plr:FindFirstChild('leaderstats')
08        local value = stats:FindFirstChild('Money')
09        value.Value = value.Value + 100
10        wait(5)
11        debounce = 0
12end

Thanks, JailBreaker_13

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. Dev_Zephyr -5 — 4y
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. JailBreaker_13 350 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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:

01local debounce = false
02 
03script.Parent.Touched:Connect(function(hit)
04    local plr = player:GetPlayerFromCharacter(hit.Parent)
05 
06    if not debounce and plr then
07        debounce = true
08        local stats = plr.leaderstats
09        local value = stats.Money
10        value.Value = value.Value + 100
11        wait(5)
12        debounce = false
13    end
14end
0
Thanks for your help. (Some errors were because i am on mobile.) JailBreaker_13 350 — 4y
0
Alright and no problem. youtubemasterWOW 2741 — 4y
0
This did not work. JailBreaker_13 350 — 4y
Ad

Answer this question