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

Why is this a 'nil value'? [closed]

Asked by 9 years ago

Two errors: Players.Player1.PlayerGui.ScreenGui.Points.Text:2: attempt to index field 'points' (a nil value)

Players.Player1.PlayerGui.MouseScripts.MouseS:10: attempt to index field 'points' (a nil value)

Number 1 This script is supposed to change this TextLabel named 'Points' text to the number of points you have.

script.Parent.Text = _G.points.Value.. " Points"

_G.points.Changed:connect(function()
    script.Parent.Text = _G.points.Value.. " Points"
end)

Number 2 This script controls the points, and how you get them. I'm not sure what the error means.

local mouse = game.Players.LocalPlayer:GetMouse()
local keyName = 'GameRock'

mouse.Button1Down:connect(function()
    local t = mouse.Target
    if t and t.Name == keyName then
        t:Destroy()
        _G.points.Value = _G.points.Value + 100
    end
end)

Here is the script to _G.points:

game.Players.PlayerAdded:connect(function(nP)
    _G.points = Instance.new("IntValue",workspace)
    _G.points.Name = nP.Name.." Points"
end)

Locked by Goulstem, NinjoOnline, and Redbullusa

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

There are a couple problems with this


1) - When you set the 'points' value inside of the PlayerAdded event, then it is going to get overwritten every single time a player joins. Meaning it is going to be the points for whatever player joined last.

2) - Localscript _G tables, and serverscript _G tables differentiate from one another, so you can't use it in a localscript. And I know that in that second script it's a localscript because you can only access the client's mouse from a localscript.


So what you need to do is just have the points inside the player, I know this is probably why you were doing this silly _G stuff anyways because why else would you.. but it's just going to be easier and there's no reason not to.


Script 1(make it a localscript) -

script.Parent.Text = _G.points.Value.. " Points"

local plr = game.Players.LocalPlayer
local stat = plr.Points

stat.Changed:connect(function(change)
    script.Parent.Text = tostring(change).." Points"
end)

Script 2(make it a localscript) -

local plr = game.Players.LocalPlayer
local points = plr.Points
local mouse = plr:GetMouse()
local keyName = 'GameRock'

mouse.Button1Down:connect(function()
    local t = mouse.Target
    if t and t.Name == keyName then
        t:Destroy()
        points.Value = points.Value + 100
    end
end)

Script 3(make it a serverscript) -

game.Players.PlayerAdded:connect(function(plr)
    local p = Instance.new('IntValue',plr)
    p.Name = 'Points'
end)
0
Thanks! Really helped. Senor_Chung 210 — 9y
0
No problem(: Goulstem 8144 — 9y
Ad