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

Attempt to index nil with 'leaderstats'?

Asked by
CodeWon 181
3 years ago
Edited 3 years ago

I was trying to make it so you can sell your gems for 5 coins each, and I got an error. The leaderstats work perfectly fine as well.

script.Parent.MouseButton1Click:Connect(function(player)
    player.leaderstats.Coins.Value = player.leaderstats.Gems.Value * 5

    player.leaderstats.Gems.Value = 0

    print(player.Name.." has sold their gems")
end)

Also would this be a local script or a script?

Thank you!

The error is on line 2

1
Is this a gui or a part Omerevkizel 27 — 3y
0
its a gui CodeWon 181 — 3y
1
Where is this gui? Is it a ScreenGui or a SurfaceGui? Please provide some more context on the situation, otherwise writing a solution is hard. And don't follow what Omer is saying, since you don't use a localscript to modify leaderstats. That won't be replicated amongst the server and other clients. Dalamix 26 — 3y
0
the gui is a screen gui, its a text button CodeWon 181 — 3y

3 answers

Log in to vote
3
Answered by
MattVSNNL 620 Moderation Voter
3 years ago
Edited 3 years ago

Also if you only sell with a local script it won't permanently change since its only local there a big difference between local and server, It works with filtering enabled, Basically if you don't know what a remote event does, You use a remote event for filtering enabled, For example, you want to add leader stats to a player, you can fire it from a local script to a server plus sending data like the player to the server, So it will be permanent because everything changed for a server will be for everyone, Hope this helped!

How to do it! :

add in a remote event

Put this script as a local script into your Gui

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer()
end)

add a server script into the server script service

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) -- Always when using remote events or remote functions add player into it

    player.leaderstats.Coins.Value = player.leaderstats.Gems.Value * 5
    player.leaderstats.Gems.Value = 0

    print(player.Name.." has sold their gems")
end
0
Omg thanks for the accept MattVSNNL 620 — 3y
Ad
Log in to vote
1
Answered by
Dalamix 26
3 years ago

This could be done in a lot of different ways, but here is my take on it. It might not be the most secure way of doing this, but I am not an expert and I just want to help out another fellow member of this community.

Inside the scripts I provide some explanations to how this system works.

In ReplicatedStorage I want you to put a RemoteEvent and name it "sellGems". Because of Roblox's way of keeping your games more secure, we need this for the client's script to be able to contact the server script which will then change the leaderstats values.

In the ServerScriptService I want you to put a normal script, name it whatever you want but it doesn't really matter.

In your gui inside of your button I want you to put a localscript, name doesn't matter here either.

Now put this code in the local script.

--Variables--
local rs = game:GetService("ReplicatedStorage") 
local sellGemsEvent = rs:WaitForChild("sellGems") 


--Functions--
function onClick() 
    sellGemsEvent:FireServer() --This is gonna send an event signal via our new event to the server-side script in ServerScriptService.
end


--Events--
script.Parent.MouseButton1Click:Connect(onClick) --Runs the function onClick() we made earlier.

Put this code in the server-side script.

--Variables--
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("sellGems")

--Functions--
function sellGems(plr) --plr is the one who fired the event, which triggered this function.
    plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value * 5 --Like what you wrote in your script, but now it's server-side so it should work.
    plr.leaderstats.Gems.Value = 0 --Like what you wrote in your script, but now it's server-side so it should work.
end


--Events--
event.OnServerEvent:Connect(sellGems) --Runs the function called sellGems(). It also provides information on who sent the event.

Like I said, read the comments on the scripts where I explain what the code does. Hopefully this works! If you still get errors, check if the leaderstats work correctly and if your values are correctly named and capitalized.

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
script.Parent.MouseButton1Click:Connect(function()
    local player = game.Players.LocalPlayer
    player.leaderstats.Coins.Value = player.leaderstats.Gems.Value * 5

    player.leaderstats.Gems.Value = 0

    print(player.Name.." has sold there gems")
end)

make it local script otherwise it will not work in any conditions done

Answer this question