DoTheDarkoSwing
DoTheDarkoSwing

Reputation: 15

Attempt to call a Instance value

This error keeps popping up in output, and I'm not sure what I am doing wrong.

Here's the code:

function bullet() --function giving me errors
    bullet = Instance.new("Part") --instance
    bullet.Size = Vector3.new(1,1,1)
    bullet.Position = script.Parent.Position
    bullet.Parent = workspace
    bullet.Velocity = script.Parent.CFrame.LookVector * 90
    bullet.CanCollide = false
    --dont think making the script disable then enable after a certain time will work
end

script.Parent.Parent.Activated:Connect(function() --the script is in the handle of the tool
    bullet() -- where the error is
    --weird thing is, i have made functions like "explode()" that worked just like this and did not have any errors
    --pretty sure this is a roblox problem, but i am not sure
    --someone help
end)

the error: "Workspace.Tool.Handle.Script:12: attempt to call a Instance value"

Upvotes: 0

Views: 1517

Answers (3)

AeteRnuM
AeteRnuM

Reputation: 1

Did you try changing the name of the function or the variable? Maybe that's the problem.

Try:

function CreateBullet() --function giving me errors
    bullet = Instance.new("Part") --instance
    bullet.Size = Vector3.new(1,1,1)
    bullet.Position = script.Parent.Position
    bullet.Parent = workspace
    bullet.Velocity = script.Parent.CFrame.LookVector * 90
    bullet.CanCollide = false
end

script.Parent.Parent.Activated:Connect(function()
    CreateBullet()
end)

I'm sorry if it didn't work or if I wasn't helpful.

Upvotes: 0

Piglet
Piglet

Reputation: 28950

--pretty sure this is a roblox problem, but i am not sure

Not the cause is your code.

You define a global function named bullet

function bullet()
-- more code
end

The first thing you do in that function is

bullet = Instance.new("Part")

Doing that you assign the return value of Instance.new("Part") to bullet.

Let's call function bullet the first time bullet()

Now the function is executed and bullet becomes an Instance. It is not a function anymore.

If we now call bullet a second time we get the error

"Workspace.Tool.Handle.Script:12: attempt to call a Instance value"

So bullet basically overwrites itself so it can not be called a second time.

Just use different names to avoid this. Then you should think about the scope. If you want to create a new bullet every time you call bullet you need to make the Intance value local as you would otherwise overwrite the same global bullet every time you call the function.

Try something like

function SpawnBullet() --function giving me errors
    local bullet = Instance.new("Part") --instance
    bullet.Size = Vector3.new(1,1,1)
    bullet.Position = script.Parent.Position
    bullet.Parent = workspace
    bullet.Velocity = script.Parent.CFrame.LookVector * 90
    bullet.CanCollide = false
    return bullet
end

script.Parent.Parent.Activated:Connect(function()
    SpawnBullet()
end)

Upvotes: 3

user18227946
user18227946

Reputation: 11

Try naming the bullet variable inside the bullet function something besides bullet or change the name of the function

Upvotes: 0

Related Questions