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