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

Why can't I check a players leaderstats with this script?

I am trying to get a part to check a players leaderstats upon being touched, but for some reason it errors on line 8. Could someone help me figure out why that is?

local Players = game:GetService("Players")
local debounce = false

function onTouch(hit)
  if debounce == false then
    debounce = true
    
     local plr = game.Players(hit.Parent.Name)
     local QuestNum = plr.leaderstats.QuestValue.Value
    
    if QuestNum == 1 then
      QuestNum = QuestNum + 1
      plr.leaderstats.XP.Value = plr.leaderstats.XP.Value + 20
    else
      return
    end
    wait(1)
    debounce = false
  else
    return
  end
end

script.Parent.Touched:Connect(onTouch)

2 comments

0

What's the error?

0

attempt to call a userdata value, - line 8 function ontouch

1 answer

Everything you wrote in this script is fine, except for one thing. When trying to find the child of Players, you used parenthesis. This would yield an error. Instead, try changing the line

local plr = game.Players(hit.Parent.Name) to: local plr = game.Players[hit.Parent.Name]. You could also change it to this: local plr = game.Players:FindFirstChild(hit.Parent.Name)

Also, make sure to use proper indentation in your script.

3 comments

0

The error is the same, except now it's line 11.

0

Oops sorry I completely for got how to heck lua worked. Let me edit this with an explanation as to why it wasn't working

0

There you go. That should work now.

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