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

What's wrong with this code?

Asked by 9 years ago

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

Log in to vote
0
Answered by 9 years ago

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

0
I tested it out, and I keep getting an error that it's "unable to find module asset id" in the output. Laserpenguin12 85 — 9y
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 koolkid8099 705 — 9y
Ad
Log in to vote
0
Answered by
Myaxp 3
9 years ago

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

0
Mate, brackets are unnecessary if it's a simple string line "Part" fahmisack123 385 — 9y

Answer this question