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

Impossible GUI to create?

Asked by 10 years ago

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 ~

ID = 158694379
badge = game:GetService("BadgeService")
player = script.Parent.Parent.Parent.Parent.Parent

function find(userid, badgeid)
if game:GetService("BadgeService"):UserHasBadge(userid, badgeid) then
    clone = game.Lighting.GravityCoil:clone()
    clone.Parent = player.StarterGear
    end
end

function MouseClick(hit)
    if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
        local p = game.Players:GetPlayerFromCharacter(hit.Parent)
        if (p ~= nil) then
            if (find(p.userId, badgeholder.Value)) then
                clone = game.Lighting.GravityCoil:clone()
                clone.Parent = player.StarterGear
        elseif (find(p.userId, badgeholder.Value) == false) then
            hit.Parent.MouseButton1Click:remove()
            end
end
script.Parent.MouseButton1Click:connect(MouseClick)

1 answer

Log in to vote
0
Answered by 10 years ago

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.

local ID = 158694379 --Local variables are generally better practice because they allow for faster lookup and limit variable clutter between functions
local badge = game:GetService("BadgeService")
local player = game.Players.LocalPlayer --an alternative for getting the player only in local scripts

function find(userid, badgeid)
    if badge :UserHasBadge(userid, badgeid) then
        return true --replaces wherever the function was called with the bool "true"
    else return false --same as above with "false
    end
end

function MouseClick()
    --if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then --this line and the two following lines are unnecessary when using a gui
    --local p = game.Players:GetPlayerFromCharacter(hit.Parent)
    --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.
    if (find(player.userID, ID)) then
        local clone = game.ReplicatedStorage.GravityCoil:clone() --ReplicatedStorage was designed to replace lighting as a storage box for items that need to be accessed by a client. I suggest putting the gravity coil there.
        local clone2 = game.ReplicatedStorage.GravityCoil:clone()
        clone.Parent = player.Backpack --adding it to the backpack and StarterGear gives the player the gear immediately and allows the player to keep it every time they die. By omitting the backpack in your original code, you require the player to die before obtaining the gear. 
        clone2.Parent = player.StarterGear
    else
        script.Parent.MouseButton1Click:Destroy() --remove() is depreciated and I took out p
        --script.Parent.BackgroundColor3 = Color3.New(.5, .5, .5) --The color is intended to be a darker shade of the normal background color of the button. This is suggested so that the player will know what they can get, and to reduce the amount of work required to make sure that the guis appear as intended.
    end
end

script.Parent.MouseButton1Click:connect(MouseClick)

Or, condensed to one function (minus comments)

local ID = 158694379
local badge = game:GetService("BadgeService")
local player = game.Players.LocalPlayer

function find(userid, badgeid)
    if badge :UserHasBadge(userid, badgeid) then
        local clone = game.ReplicatedStorage.GravityCoil:clone()
        local clone2 = game.ReplicatedStorage.GravityCoil:clone()
        clone.Parent = player.Backpack
        clone2.Parent = player.StarterGear
    else
        script.Parent.MouseButton1Click:Destroy()
        --script.Parent.BackgroundColor3 = Color3.New(.5, .5, .5)
    end
end

script.Parent.MouseButton1Click:connect(function() find(player.userId, ID) end)
0
The scripts you provided seem very reasonable but they don't appear to work. I've placed the script within a local script and then placed it within an ImageButton in my GUI. Once I entered the game and tested it, nothing happened. My profile obviously has the badge, I made sure it was the correct badge ID too. I made sure the GravityCoil was in the ReplicatedStorage as well. Does it have something HurricaneOtto 0 — 10y
0
Does it have something to do with the script being local? Will it not allow badges that aren't created for the game the script is within? I was hoping that it would allow that as the game is really 5 games conjoined into one. Is there a way to fix this? HurricaneOtto 0 — 10y
0
I just checked the wiki and it said that the badge service can only be used in a script. GoldenPhysics 474 — 10y
0
I already knew that. But does it have something to do with the badge not being created for the game the script is in? I'm only asking because the script doesn't work. HurricaneOtto 0 — 10y
Ad

Answer this question