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

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

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

5 comments

0

can you include this in a code block

0

Alright, Done.

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

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)

0

local player is not accessable by serverscript.. to define local player you have to use a local script like : game.Players.LocalPlayer

1 answer

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

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