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

How to allow player to buy a gun?

Asked by 5 years ago
Edited by youtubemasterWOW 5 years ago

Not gonna lie, my language is C#, not Lua, so I've just yolo'd this project and I need help with something. I'm making a game (obv), and I need to make it where the player can buy this AR when they click it. Here's my code that isn't working:

01Player = game.Players.LocalPlayer
02 
03script.Parent.ClickDetector.MouseClick:Connect(function()
04 
05    if Player.LeaderBoard.Money.Value >= 500 then
06        Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500
07 
08        game.ReplicatedStorage.M18:clone().Parent = Player:WaitForChild("Backpack")
09    else
10 
11    end    
12end)

Please help.

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Hello.


Problem:

  • You used LocalPlayer in a ServerScript. This will not work as LocalPlayer only works on LocalScripts.

Solution:

  • Use the player argument of ClickDetector.MouseClick instead of LocalPlayer.

Recommendations:

  • Use Clone() with an uppercase "C" as clone() is deprecated. It is never a good idea to use deprecated functions.

  • You do not have to use WaitForChild() on the player's backpack as it will be loaded in.

  • Use GetService() when indexing services as it checks if the service is existent. If it isn't it creates the service. GetService() is also useful if someone renamed the services. GetService() is the only way to create services.

  • You do not need the else for the if statement on line 02.


Fixed Script:

1script.Parent.ClickDetector.MouseClick:Connect(function(Player)
2    if Player.LeaderBoard.Money.Value >= 500 then
3        Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500
4        game:GetService("ReplicatedStorage").M18:Clone().Parent = Player.Backpack
5    end    
6end)

Also, put local before declaring a variable unless you're using a global variable. Local variables are faster and more efficient than global ones though.

0
thank you! been trying to do this FOREVER mohawk2005ishacked12 13 — 5y
0
No problem. Please accept my answer. youtubemasterWOW 2741 — 5y
0
how do i do that? lmao mohawk2005ishacked12 13 — 5y
0
For new users, there's been a bug going around that you have to go into incognito mode to accept an answer. The button should be under my answer. youtubemasterWOW 2741 — 5y
0
ahh, yeah, had to go incog. mohawk2005ishacked12 13 — 5y
Ad

Answer this question