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

Why is my Clone not running an attached script when spawned in the workspace via a localscript?

Asked by 5 years ago

Hey Everyone! I've been really into learning about programming in Roblox and I've made some progress but I'm still somewhat of a noob and I'm in need of help.

I am trying to program a Platform that a Player can spawn in front of them and click on it to make it disappear.

The Platform is named "CloneBlock2" and is stored in StarterPlayer, it has a ClickDetector and a script that programs it to be Destroyed when Clicked on. The Script on the Platform.

local Player = game.Players.LocalPlayer

    local function onMouseClick(player)

        script.Parent:Destroy()

end
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)

The Platform works If I play the game with the Platform already in the Workspace when I start the game. But If I store the Platform in StarterPlayer and spawn a clone of the Platform using a local script in StarterGui it doesnt run the script that's attached to the Platform. Not sure what I am doing wrong. Hopefully someone can help me with this.

Here is the StarterGui script.

local Player = game.Players.LocalPlayer

local Character = Player.Character

local CloneBlock2 = game.StarterPlayer.CloneBlock2


local Mouse = Player:GetMouse()


Mouse.KeyDown:connect(function(Key)

Key = Key:lower()

if Key == '2' then

CloneBlock2.CFrame = Player.Character.Head.CFrame * CFrame.new(0,0,-5)

local Clone2 = CloneBlock2:Clone()

Clone2.Parent = game.Workspace

end

end)

Formatting is off when I copy pasted code. Sorry about that, I don't know how to properly copy/paste code here from Roblox and keep formatting.

Anyhow, I would greatly appreciate some advice on how to get this working or if there's altogether a better more efficient way to do this then I'd love to learn about it.

1 answer

Log in to vote
0
Answered by 5 years ago

The reason the script isn't running is because it is created client-side, by the local script. The difference between LocalScripts and Scripts is who executes them. The client is the only one that runs LocalScripts and the server is the only one that runs Scripts.

If you have a LocalScript create a Script, it is created locally. The client will not run this and the server doesn't know it exists so it can't run it. If you were to create the platform on the server, it is the server creating it. That however syncs it to other clients too. But it should work that way as the server can see it and run the code.

The reason it worked when you just had it in Workspace is because it is part of the world the server sends to clients. It can see the script and execute it.

Ad

Answer this question