Cannot get script from an entity with raycast

var from =;
var to = from.clone();
to.add(;, to, function (result) {
    if( === "blockTop" || === "blockBottom" || ===   "blockRight" || === "blockLeft" || === "blockFront" || === "blockBack"){
        target = result.entity.getParent().findByName("Test Block");
        //Here is where the code to lower the blocks health should go...

Each block is surrounded by six entities which act as the sides of the block. When one of the sides is hit, the target value is set to the actual block itself. Each block should also have a script telling it how much health is should have. As the player attacks the block, it should start loosing health. The problem is I do not know how to get the health value out of the blocks script.

I have tried:

var blocksHealth = target.script.Block.blockHealth;
//When block is struck by raycast:
blocksHealth -= 1;
if(blocksHealth <= 0){

And on the script “Block” located on the block itself, in the “initialize” section:

var blockHealth = 100;

It gives me an error:
cannot read property blockHealth of undefined.

What am I doing wrong?

That error message means that there is no property on target.script called Block. Are you sure your script name is Block? Alternatively, are you sure the entity target actually has the Block script assigned to it?

Yes, the script is named “Block” and the block has that script attatched to it.


Block is sopposed to be the name of the script correct?

It’s the string passed to pc.scriptCreate. e.g.:

var Block = pc.scriptCreate('block');

In that case, you should do:

var health =;

Note the lower case ‘b’.

thanks the script is working now, even added a system where the blocks don’t break instantly, but actually have health.

1 Like