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

What's wrong with line 19??

Asked by 10 years ago

When I tested this in Studio (Tools-Test-StartServer, and Player), I got an error for line 19...:

attempt to index local 'player' (a nil value)

Code:

--SETUP--
ToolName = "One" --Exact Tool Name here.
Price = 0 --Price of the tool
Currency = "Cash" --Currency Type
--SETUP COMPLETE--

Button = script.Parent
ds = game:GetService("DataStoreService")
function CheckIfBought(name,Name)
    dss = ds:GetDataStore(Name.." KM Tools")
    if dss:GetAsync(name)==true then
        return true
    else
        return false
    end
end

function Buy(player)
    dss = ds:GetDataStore(player.Name.." KM Tools")
    stats = player.leaderstats
    currency = stats:FindFirstChild(Currency)
    a = game.Lighting.Tools:FindFirstChild(ToolName)
    if (CheckIfBought(ToolName,player.Name) == true) then --If the player already bought the tool...

        if a ~= nil then
            a:Clone().Parent = player.Backpack
        end
    else
        if currency ~= nil and currency.Value >= Price then
            currency.Value = currency.Value - Price
            a:Clone().Parent = player.Backpack
            dss:SetAsync(ToolName,true)
        end
    end
end
Button.MouseButton1Click:connect(Buy)

P.S. It would also be helpful if you could tell me if this code was to work with the error being fixed. Thanks!

2 answers

Log in to vote
1
Answered by 10 years ago

Okay, so your problem is that the MouseClick event, I believe, doesn't return the player, it returns (EDIT: It returns absolutely nothing). Therefore you are attempting to use the nothing's name, which will return nil.

To fix this, you could access the player via the PlayerGui, where I would assume your script is located.

Edited code:

--SETUP--
ToolName = "One" --Exact Tool Name here.
Price = 0 --Price of the tool
Currency = "Cash" --Currency Type
local PlayerGui = script.Parent.Parent.Parent --assuming the button is inside of a ScreenGui which inside of the PlayerGui
local player = PlayerGui.Parent
--SETUP COMPLETE--

Button = script.Parent
ds = game:GetService("DataStoreService")
function CheckIfBought(name,Name)
    dss = ds:GetDataStore(Name.." KM Tools")
    if dss:GetAsync(name)==true then
        return true
    else
        return false
    end
end

function Buy()
    dss = ds:GetDataStore(player.Name.." KM Tools")
    stats = player.leaderstats
    currency = stats:FindFirstChild(Currency)
    a = game.Lighting.Tools:FindFirstChild(ToolName)
    if (CheckIfBought(ToolName,player.Name) == true) then --If the player already bought the tool...

        if a ~= nil then
            a:Clone().Parent = player.Backpack
        end
    else
        if currency ~= nil and currency.Value >= Price then
            currency.Value = currency.Value - Price
            a:Clone().Parent = player.Backpack
            dss:SetAsync(ToolName,true)
        end
    end
end
Button.MouseButton1Click:connect(Buy)

That should work, though I am very inexperienced with DataStore things.

0
The MouseButton1Click event passes absolutely nothing. Tkdriverx 514 — 10y
0
Ah, okay, I wasn't sure if it was nothing, or the mouse. Thanks for that. SlickPwner 534 — 10y
Ad
Log in to vote
0
Answered by
Relatch 550 Moderation Voter
10 years ago

In this script, you didn't define player. That is your problem. To define player, I would recommend adding this to your code/script.

local player = game.Players.LocalPlayer

The above explains what player is, so the script knows what you're talking about on line 19 near "player.Name".

Also, when you don't define what something means and it is used on more than one line(like this code/script), than it would not work on those lines.

1
game.Players.LoaclPlayer is a LocalScript. This is a ServerScript since DataStores only work on ServerScripts. fahmisack123 385 — 10y

Answer this question