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

What did I do wrong in this script? It keeps saying Attempted to call a nil value.

Asked by 4 years ago
Edited 4 years ago

So I'm working on an RPG and I'm trying to make a shop GUI like in RPG World or Slaying Simulator. I am a noob at scripting so if you know the answer, please tell me what I did wrong. Script: In the out

local buyer = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect()
if buyer.leaderstats.Gold.Value >= 100 and script.Parent.MouseButton1Click:connect() then
    game.ServerStorage.Folder.Sword:Clone().Parent = game.Players.LocalPlayer.Backpack
end

if buyer.leaderstats.Gold.Value <= 100 and script.Parent.MouseButton1Click:connect() then
    return end
0
can you include this in a code block User#5423 17 — 4y
0
Alright, Done. PazVan800 -5 — 4y
0
This needs to be moved to a server script as that is where you manage player stats. you would need to look at remote events if you have not already done so User#5423 17 — 4y
0
As I said, I SUCK at scripts and i changed it to a different script, made a remote event and put it in server script service. Now it says: 16:07:33.366 - ServerScriptService.Script:4: attempt to index field 'LocalPlayer' (a nil value) PazVan800 -5 — 4y
0
local player is not accessable by serverscript.. to define local player you have to use a local script like : game.Players.LocalPlayer Yuuwa0519 197 — 4y

1 answer

Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

To get the player, I'm assuming this is a GUI and since it gets cloned into the PlayerGui you can just go up in the hierarchy by using script.Parent until you get the player object.

Let's look at your code:

-- local buyer = game.Players.LocalPlayer -- can't use this in a server script

--assuming the hierarchical structure is ScreenGUI -> Frame -> Button -> Script

local buyer = script.Parent.Parent.Parent.Parent.Parent -- getting the player

script.Parent.MouseButton1Click:connect()
    if buyer.leaderstats.Gold.Value >= 100 then -- can't use events in an if statement
        game:GetService("ServerStorage").Folder.Sword:Clone().Parent = buyer.Backpack
    end
end) -- Ending the mouse click event

-- remove code below
if buyer.leaderstats.Gold.Value <= 100 then -- Why is this here? And again, you can't use click events in an if statement
    return end -- this doesn't work
Ad

Answer this question