I've been trying my best to make a GUI with weapon equip options. However, the weapons in the game must be found via Badge collection. So once the badge for the weapon is found, you should be able to click on the GUI button and equip it to the players inventory, if it isn't found then the equip button should be disabled. I'm confident I'm just following the wrong guidelines to make it, any options for this?
My sad attempt ~
01 | ID = 158694379 |
02 | badge = game:GetService( "BadgeService" ) |
03 | player = script.Parent.Parent.Parent.Parent.Parent |
04 |
05 | function find(userid, badgeid) |
06 | if game:GetService( "BadgeService" ):UserHasBadge(userid, badgeid) then |
07 | clone = game.Lighting.GravityCoil:clone() |
08 | clone.Parent = player.StarterGear |
09 | end |
10 | end |
11 |
12 | function MouseClick(hit) |
13 | if (hit.Parent:FindFirstChild( "Humanoid" ) ~ = nil ) then |
14 | local p = game.Players:GetPlayerFromCharacter(hit.Parent) |
15 | if (p ~ = nil ) then |
The main problems: MouseButton1Click doesn't return any parameters, hit is mainly for touched events; putting the grav coil clone into StarterGear requires the player to die before obtaining the gear, which I assume to be undesired; if the MouseClick function worked, by calling the find function, the player will receive the gear twice.
01 | local ID = 158694379 --Local variables are generally better practice because they allow for faster lookup and limit variable clutter between functions |
02 | local badge = game:GetService( "BadgeService" ) |
03 | local player = game.Players.LocalPlayer --an alternative for getting the player only in local scripts |
04 |
05 | function find(userid, badgeid) |
06 | if badge :UserHasBadge(userid, badgeid) then |
07 | return true --replaces wherever the function was called with the bool "true" |
08 | else return false --same as above with "false |
09 | end |
10 | end |
11 |
12 | function MouseClick() |
13 | --if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then --this line and the two following lines are unnecessary when using a gui |
14 | --local p = game.Players:GetPlayerFromCharacter(hit.Parent) |
15 | --if (p ~= nil) then --and if you are using this, then "if p then" is fine. If p is nil, it'll return nil or false and not run the code under that if statement. |
Or, condensed to one function (minus comments)
01 | local ID = 158694379 |
02 | local badge = game:GetService( "BadgeService" ) |
03 | local player = game.Players.LocalPlayer |
04 |
05 | function find(userid, badgeid) |
06 | if badge :UserHasBadge(userid, badgeid) then |
07 | local clone = game.ReplicatedStorage.GravityCoil:clone() |
08 | local clone 2 = game.ReplicatedStorage.GravityCoil:clone() |
09 | clone.Parent = player.Backpack |
10 | clone 2. Parent = player.StarterGear |
11 | else |
12 | script.Parent.MouseButton 1 Click:Destroy() |
13 | --script.Parent.BackgroundColor3 = Color3.New(.5, .5, .5) |
14 | end |
15 | end |
16 |
17 | script.Parent.MouseButton 1 Click:connect( function () find(player.userId, ID) end ) |