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

onClicked Give Player Rice?

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)

4 comments

0

What exactly is the problem?

0

It says that humanoid is nil

0

Why do you have a capital F for FindFirstChild, but the rest for findFirstCild are lowercase? Just wondering.

0

Typo, but that doesn't fix it. Thanks for pointing it out, though, will help!

2 answers

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)

2 comments

0

Thanks so much!

0

Btw remember to accept this answer and NP!

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)

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