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

onClicked Give Player Rice?

Asked by 4 years ago

I am trying to create a block of rice, that when you click, will give you rice. This does not seem to work, and I have looked all over for answers. I even found something similar on this site, but could not get it to work. Any help is appreciated!

function onClicked(clicked)
    local h = clicked.Parent:FindFirstChild("Humanoid")
    local l = h:findFirstChild("leaderstats")
    local d = l:findFirstChild("Rice")
    script.Parent.CFrame = script.Parent.CFrame*CFrame.new(0,-1,0)
    script.Parent.Level.Value = script.Parent.Level.Value -1
    d.Rice.Value = d.Rice.Value + 1
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
What exactly is the problem? Prestory 1395 — 4y
0
It says that humanoid is nil Raidhavoc 0 — 4y
0
Why do you have a capital F for FindFirstChild, but the rest for findFirstCild are lowercase? Just wondering. Aqu_ia 39 — 4y
0
Typo, but that doesn't fix it. Thanks for pointing it out, though, will help! Raidhavoc 0 — 4y

2 answers

Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
4 years ago
Edited 4 years ago

The humanoid is returning nil because the clicked argument returns the player not the character which means the script is searching the player for the Humanoid but the character contains the Humanoid to fix this all you have to do is search the character for the humanoid like i have done below for you.

function onClicked(clicked)
    local Character = workspace:WaitForChild(clicked.Name)
    local h = Character:WaitForChild('Humanoid')
    local l = clicked:WaitForChild("leaderstats")
    local d = l:FindFirstChild("Rice")
    script.Parent.CFrame = script.Parent.CFrame*CFrame.new(0,-1,0)
    script.Parent.Level.Value = script.Parent.Level.Value -1
    d.Rice.Value = d.Rice.Value + 1
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
Thanks so much! Raidhavoc 0 — 4y
0
Btw remember to accept this answer and NP! Prestory 1395 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Thank you so much to https://scriptinghelpers.org/user/Prestory! With a bit of tweaking, I was able to get it to work! Here is the final script in case anyone is wondering!

script.Parent.ClickDetector.MouseClick:connect(onClicked)

function onClicked(clicked)
    local Character = workspace:WaitForChild(clicked.Name)
    local h = Character:WaitForChild('Humanoid')
    local l = clicked:WaitForChild("leaderstats")
    local d = l:FindFirstChild("Rice")
    script.Parent.CFrame = script.Parent.CFrame*CFrame.new(0,-1,0)
    script.Parent.Level.Value = script.Parent.Level.Value -1
    d.Value = d.Value + 1
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Answer this question