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

What's wrong with this code?

I'm trying to create a instance so a part in my game will spawn when another part is click, here's what I have so far...

local ball = game.Workspace.Fifa Ball

function onClicked()
	local brick = Instance.new("Part", game.Workspace)
	brick.Name = "Fifa Ball"
	
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

Ok, i think i understand what you are trying to do. And i will tell you where you went wrong. When declaring variables there are two types, Global and Local. Local variables can only be used when inside of a function. So that was the first Mistake. Secondly, if you have a space in the object declared in the variable, its slightly more complicated. it should look like this if you want a space

ball = game.Workspace.["Fifa Ball"]

It is much easier to use no spaces. The final product should turn out like this


function onClicked()
    local brick = Instance.new("Part", game.Workspace)
    brick.Name = "FifaBall"

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

You can put this script inside any part along wit ha ClickDetector for it to work. You can even take steps further and manipulate the location of the brick upon its spawning.

****Hope This helped you :D

2 comments

0

I tested it out, and I keep getting an error that it's "unable to find module asset id" in the output.

0

what is the script's parent. when i tried to do this script it worked fine for me. also try clearing output and look inside workspace to see if it is even spawning in

2 Problems

Problem 1:

You variable is not meant to have a gap. The script reads it as FifaBall rather than Fifa Ball. The way to do this is by adding [ ] with a string inside of it... Like so.

local ball = game.Workspace["Fifa Ball"] -- Remember, there is no "." between "workspace" and ["Fifa Ball"]

Problem 2:

The other thing is. Naming a part should be in brakets. Using ( ) with a string inside will work.

local ball = game.Workspace["Fifa Ball"]

function onClicked()
    local brick = Instance.new("Part", game.Workspace)
    brick.Name = ("Fifa Ball")

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Hoped it helped!

--//Myaxp

1 comment

0

Mate, brackets are unnecessary if it's a simple string line "Part"

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