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

How to fix attempt to call field 'Name' (a string value)?

Asked by 5 years ago

Can someone help?Since I'm trying to make a viewmodels and randomly this error popped up

Error:

  20:06:58.185 - Players.wiktormont.Backpack.Tool.LocalScript:11: attempt to call field 'Name' (a string value)
20:06:58.187 - Stack Begin
20:06:58.200 - Script 'Players.wiktormont.Backpack.Tool.LocalScript', Line 11
20:06:58.201 - Stack End

Script:

local Cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local ReplicatedS = game.ReplicatedStorage
local Mouse = player:GetMouse()
local Tool = script.Parent
local RunService = game:GetService("RunService")




local  V_Model = "V_" ..Tool.Name ""


for i,v in pairs (ReplicatedS.V_Model:GetChildren()) do
    if v:IsA("BasePart") then
        if v ~= V_Model.PrimaryPart then
        local Weld = Instance.new("Weld")
        Weld.Part0 = V_Model.PrimaryPart
        Weld.Part1 = v
        Weld.C0 = V_Model.PrimaryPart.CFrame:inverse()
        Weld.Name = v.Name
        Weld.Parent = V_Model.PrimaryPart
        end
    end
end


Tool.Equipped:Connect(function(Mouse)
    V_Model.Parent = Cam
    RunService.RenderStepped:Connect(function()
        V_Model:SetPrimaryPartCFrame(Cam.CFrame * CFrame.new(0,-1.5,1.5))
    end)
end)

1 answer

Log in to vote
1
Answered by 5 years ago

You forgot to concatenate tool.Name with the empty string.

Look at line 11.

local  V_Model = "V_" ..Tool.Name ""

In Lua, you can call a function without parenthesis if the argument passed is a string literal or a table literal.

So these examples are just fine:

print "Hello world!"
print{}

This is syntactic sugar for a function call.

Lua just thought you were attempting to call tool.Name; a string value.

You don't need the extra pair of ""; it's an empty string and won't affect anything.

local V_Model = "V_" ..Tool.Name 

Also, don't nest your RenderStepped listener inside your Equipped listener, each time the tool is equipped, a new connection is created, so you have who knows how many functions being called.

local Cam = game:GetService("Workspace").CurrentCamera
local player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Mouse = player:GetMouse()
local Tool = script.Parent
local RunService = game:GetService("RunService")
local  V_Model = "V_" ..Tool.Name 

for i,v in pairs(ReplicatedS.V_Model:GetChildren()) do
    if v:IsA("BasePart") then
        if v ~= V_Model.PrimaryPart then
        local Weld = Instance.new("Weld")
        Weld.Part0 = V_Model.PrimaryPart
        Weld.Part1 = v
        Weld.C0 = V_Model.PrimaryPart.CFrame:inverse()
        Weld.Name = v.Name
        Weld.Parent = V_Model.PrimaryPart
        end
    end
end


Tool.Equipped:Connect(function(Mouse)
    V_Model.Parent = Cam
end)

RunService.RenderStepped:Connect(function()
        V_Model:SetPrimaryPartCFrame(Cam.CFrame * CFrame.new(0,-1.5,1.5))
end)

If you need the tool to be equipped, simply have a variable that contains a boolean, assign the variable to false when not equipped, and true when it is equipped, kinda like so:

local equipped = false

tool.Equipped:Connect(function(mouse)
    equipped = true
    -- ...
end)

tool.Unequipped:Connect(function()
    equipped = false
    -- ...
end)

RunService.RenderStepped:Connect(function(dt)
    if (equipped) then
        -- do something
    end
end)
0
Fixes the error but the problem is that it doesn't show for some Reason wiktormont 14 — 4y
0
bad code Fifkee 2017 — 4y
0
what wiktormont 14 — 4y
Ad

Answer this question