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

Why cant this script identify my variable?

So I have a script here:

tool = script.Parent
player = game.Players.LocalPlayer
mouse = player:GetMouse()
reload = true
tool.Activated:connect(function()
	if reload == true then
		reload = false
	local Part = Instance.new("Part", workspace)
	Part.Size = Vector3.new(0.1,0.1,0.1)
	Part.Shape = ("Ball")
	Part.Position = Vector3.new(-3.5,0.5,4.5)
	Part.CFrame = tool.Handle.CFrame
	Part.CFrame = CFrame.new(Part.Position,mouse.Hit.p)
	local v = Instance.new("BodyVelocity", Part)
	v.velocity = Part.CFrame.lookVector *90
	v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
	local destroy = Instance.new("IntValue", Part)
	destroy.Value = 0
	Part.Touched:connect(function(hit)

		if hit.Name == "Snow" and Part.destroy.Value == 0 then
			destroy.Value = 1
			Part:Destroy()
			local Snow = hit
			Snow.Transparency = 0.1
			wait(0.1)
			Snow.Transparency = 0.2
			wait(0.1)
			Snow.Transparency = 0.3
			wait(0.1)
			Snow.Transparency = 0.4
			wait(0.1)
			Snow.Transparency = 0.5
			wait(0.1)
			Snow.Transparency = 0.6
			wait(0.1)
			Snow.Transparency = 0.7
			wait(0.1)
			Snow:Destroy()
			
		end	
	end)
	wait(3)
	reload = true
end
end)

The problem is that when ever I use this script, I can an error in the output that says: Players.Player1.Backpack.Tool.LocalScript:21: attempt to index field 'destroy' (a function value)

2 comments

0

Have you tried naming it something else?

0

What do you mean?

2 answers

✓ Accepted answer

"destroy" is not the name of the IntValue. destroy is the name of the variable. If you are to search through the part for a "destroy", then you're applying the variable:

local destroy = Instance.new("IntValue", Part)

That doesn't work. what you want to do is simply search it.

 if hit.Name == "Snow" and destroy.Value == 0 then -- this should simply look into the IntValue it created

A much better way of doing this would be to NAME the IntValue, and detect it. Also, there is a more efficient way of running this code

tool = script.Parent
player = game.Players.LocalPlayer
mouse = player:GetMouse()
reload = true
tool.Activated:connect(function()
    if reload == true then
        reload = false
    local Part = Instance.new("Part", workspace)
    Part.Name = "NAMETHIS" -- Name the part as well! Change this to something useful.
    Part.Size = Vector3.new(0.1,0.1,0.1)
    Part.Shape = ("Ball")
    Part.Position = Vector3.new(-3.5,0.5,4.5)
    Part.CFrame = tool.Handle.CFrame
    Part.CFrame = CFrame.new(Part.Position,mouse.Hit.p)
    local v = Instance.new("BodyVelocity", Part)
    v.velocity = Part.CFrame.lookVector *90
    v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
    local destroy = Instance.new("IntValue", Part)
    destroy.Name = "NAME" -- Name it accordingly to how you'll refer to it in other scripts.
    destroy.Value = 0

    Part.Touched:connect(function(hit)
local check = game.Workspace.NAMETHIS.NAME.Value
        if hit.Name == "Snow" and  check == 0 then 
            check.Value = 1
            game.Workspace.NAMETHIS:Destroy()
            local Snow = hit
	    for i= 1,7 do -- this will make the following code run 7 times
		Snow.Transparency = Snow.Transparency + 0.1
                wait(0.1)

            end
            Snow:Destroy()

        end 
    end)
    wait(3)
    reload = true
end
end)

What your trying to do is this in the line 21 if statement. This is what the script understands.

if hit.Name == "Snow" and Instance.new("Part", workspace).Instance.new("IntValue", Part).value == 0 then
--code
end

Try this.

if hit.Name == "Snow" and game.Workspace.Part.IntValue.Value == 0 then
--code
end

Accept answer if this helps!

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