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 4 years ago
Edited by youtubemasterWOW 4 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:

Player = game.Players.LocalPlayer

script.Parent.ClickDetector.MouseClick:Connect(function()

    if Player.LeaderBoard.Money.Value >= 500 then
        Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500

        game.ReplicatedStorage.M18:clone().Parent = Player:WaitForChild("Backpack")
    else

    end     
end)

Please help.

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 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:

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

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 — 4y
0
No problem. Please accept my answer. youtubemasterWOW 2741 — 4y
0
how do i do that? lmao mohawk2005ishacked12 13 — 4y
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 — 4y
0
ahh, yeah, had to go incog. mohawk2005ishacked12 13 — 4y
Ad

Answer this question