Sorry I’m confused. What do you mean?
As in you said hit.Parent
when you meant hit.Parent.Name
.
Also, just use :GetPlayerFromCharacter
because it is for these kinds of situations.
https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter
I just changed it all to hit.Parent instead of hit.Parent.Name. I also changed it to :GetPlayerFromCharacter
local elixir = script.Parent
local currency = "Elixir"
local amt = 1
local d = true
elixir.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and d then
d = false
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
plr("leaderstats"):FindFirstChild(currency).Value = plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value + amt
print("okay")
end
elixir:Destroy()
end
end)
Do you know why it’s causing an error at if hit.Parent:FindFirstChild(“Humanoid”)?
I think it’s because it’s hitting some parts in the workspace before the player, but then why would it error if it’s just checking to see if the the parent is a Humanoid?
According to the error, to me it looks like currency doesn’t really exists. Because in the screenshot you provided, it had the folder
leaderstats
-Dark Elixir
-Elixir
-Gold
So you probably didn’t define the variable properly? Also, FindFirstChild(“needstobeinspeechmarks”)
Could be wrong and don’t understand what your defining tho, so lemme know.
Edit: Also use the getplayerfromcharacter as other people have stated.
If you don’t understand what I meant was get rid of local currency = “Elixir” and try and define the variable a diff way
It shouldn’t do that, but what exactly is your error that you’re getting?
As for:
Maybe instead of that do something like:
if plr and plr.leaderstats:FindFirstChild(currency) then
local currency2 = plr.leaderstats:FindFirstChild(currency)
currency2.Value = currency2.Value + amt
end
Maybe it’s more complicated, so just do whatever suits you.
17:56:56.750 - Workspace.ElixirDrop.ElixirDropCollect:9: attempt to index nil with 'FindFirstChild'
Thanks it still comes up with the error but it’s working! Do you know why it works with your script and not mine?
No, I don’t know why mine works at all, but thanks anyways!
Also for your error:
After looking at some other topics about this, apparently hit.Parent
doesn’t exist, so it’s nil
.
Do you mind linking the topics. Thanks
Here:
and here:
https://devforum.roblox.com/t/music-script-attempting-to-index-nil-with-findfirstchild/661969
My thread is a different situation however
I know, but your error message is the same as she’s getting so it does help.