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

why does this script give the weirdest error?

Asked by 8 years ago
mouse = game.Players.LocalPlayer:GetMouse()
player = game.Players.LocalPlayer

local clone = game.Workspace.turret:Clone()
mouse.Move:connect(function()
    if mouse.Target ~= nil then
    if mouse.Target.Name == "m" then
        clone.Parent = game.Workspace
        clone.Anchored = true
        clone.CanCollide = false
        clone.Position = mouse.Target.Position + Vector3.new(0,1,0)
    else clone:Destroy()
        end
    end
    end)

it's supposed to make a part be cloned and put on top of a part when my mouse moves over it heres the error:

The Parent property of turret is locked, current parent: NULL, new parent Workspace line 8

What? if i take out the else part, it works fine, but then the part stays on the last part called 'm' that mouse was on. so why doesn't it work with the else?

0
It's because you destroy the clone on line 12 when the if statement is false. Therefore when the if statement of line 7 is true the clone is parented and locked to nil. M39a9am3R 3210 — 8y
0
oh, so how do i fix it, if it gets put at nil when the clone is destroyed? ScriptsAhoy 202 — 8y
0
Sorry about the long wait, try it now, i added an elseif on line 12 that might help. Hopefully we can get it working dyler3 1510 — 8y

1 answer

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

Your problem is that any time you're not clicking on the right object, the clone gets destroyed and therefore messes up any other times you try to click.

The way we could fix this by re-cloning the part before we destroy it. Here's an example of what I mean:

if mouse.Target.Name == "m" then
    --Code--
else 
    local oldclone = clone
    oldclone:Destroy()
    clone = game.Workspace.turret:Clone()
end

This would effectively destroy the part, while allowing you to do whatever you want with the new clone.


This is what it would look like in your script:

mouse = game.Players.LocalPlayer:GetMouse()
player = game.Players.LocalPlayer

local clone = game.Workspace.turret:Clone()
mouse.Move:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Name == "m" then
            clone.Parent = game.Workspace
            clone.Anchored = true
            clone.CanCollide = false
            clone.Position = mouse.Target.Position + Vector3.new(0,1,0)
        elseif mouse.Target.Name ~= "turret"
            local oldclone = clone
            oldclone:Destroy()
            clone = game.Workspace.turret:Clone()
        end
    end
end)

And there you go, it should work now.


I hope this helped, but if you still run into any problems or have any questions, please leave a comment below and I'll see what i can do.

0
so now its really glitchy, because when the part gets created onto the m part called m the mouse.Target changes to turret, which means it will destroy it, and then repeat the thing over again, making the part really glitchy ScriptsAhoy 202 — 8y
Ad

Answer this question