Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This GUI has a nil value?

local plr = game.Players.LocalPlayer
local foil = plr.Backpack:FindFirstChild("LinkedSword3").Handle.Mesh
local foil2 = plr.StarterGear:FindFirstChild("LinkedSword3").Handle.Mesh

script.Parent.MouseButton1Click:Connect(function()
    if foil.TextureId == ("rbxassetid://2150176816") or foil2.TextureId = ("rbxassetid://2150176816") then
       

        print ("Already Has This Item")
    else

        if plr.leaderstats.Wins.Value>=5 then
            foil.TextureId = ("rbxassetid://2150176816")
    foil2.TextureId = ("rbxassetid://2150176816")

            


            else
            print ("Does Not Have Enough Wins")
        end
    end
end)

The Error is "attempt to call a nil value".

The goal is to be a shop that gives sword textures, it only works without the startergear, but you can reset and delete the texture.

1 answer

✓ Accepted answer

###Just because you use FindFirstChild doesn't mean your script won't error if the script doesn't find the child. You should use if statements to verify existence. You also shouldn't check if a user has enough wins on the client, do that on the server. I will use a RemoteFunction to check this.

local plr = game.Players.LocalPlayer
local hasEnoughWins = game:GetService("ReplicatedStorage").HasEnoughWins
-- RemoteFunction

script.Parent.MouseButton1Click:Connect(function()
    local foil = plr.Backpack:FindFirstChild("LinkedSword3") -- i assigned foil to the swords themselves
    local foil2 = plr.StarterGear:FindFirstChild("LinkedSword3") 
    
    if not foil or not foil2 then return end -- if the swords were not found, stop
    
    local f = foil.Handle.Mesh
    local f2 = foil2.Handle.Mesh

    if f.TextureId == "rbxassetid://2150176816" or f2.TextureId == f.TextureId then
         -- brackets redundant 
        print ("Already Has This Item")
    else
        if hasEnoughWins:InvokeServer() then 
            -- check these things on the server, as clients can manipulate them to a minimum 
            f.TextureId = "rbxassetid://2150176816"
            f1.TextureId = f.TextureId
        else
            print ("Does Not Have Enough Wins")
        end
    end
end)

###Server:

local hasEnoughWins = game:GetService("ReplicatedStorage").HasEnoughWins

hasEnoughWins.OnServerInvoke = function(plr)
    return plr.leaderstats.Wins.Value >= 5
end

1 comment

0

Does the 'server' script go in server or serverscript storage?

This discussion is preserved as it was. New answers, comments, and votes are closed.