Need Help With Script (delete block)

Hey! I’m a new person to coding and I want to make a item that places blocks but after 2 seconds of being in workspace, they self destruct I already have the place block script thanks to one of my friends explaining a code, but I need to know how to make the block destruct itself after being in workspace for 2 seconds. Please help me out, here is the script. Thank you for your time!

function place()
p = Instance.new(“Part”)
p.Parent = game.Workspace
p.Name = game.Players.LocalPlayer.Name
p.BrickColor = game.Players.LocalPlayer.PlayerGui.Vectors.Color.Value
if game.Players.LocalPlayer.PlayerGui.Vectors.Anchor.Value == 1 then
p.Anchored = true
elseif game.Players.LocalPlayer.PlayerGui.Vectors.Anchor.Value == 0 then
p.Anchored = false
end
p.Size = game.Players.LocalPlayer.PlayerGui.Vectors.VValue.Value
p.Position = game.Players.LocalPlayer.Character.Humanoid.TargetPoint
end

script.Parent.Activated:connect(place)

Heyo, welcome to the world of scripting! :wave: After waiting for 2 seconds by using wait(2) or task.wait(2), you can then destroy the p instance (Which would be your part)

There is also a couple things to note in relation with your script, if you wouldn’t mind me going through all of them :thinking:

Most of these lines won’t be able to be accessed because you’re attemping to obtain a LocalPlayer variable, which can only be used inside LocalScripts (If this already is one, do consider adding variables to make it easier to reference what you’re exactly changing :slightly_smiling_face:)

local Tool = script.Parent --Is this a tool?
local function Place()
    local Char = Tool.Parent
    local Plr = game.Players:GetPlayerFromCharacter(Char)

    if Char and Plr then
        local Humanoid = Char:WaitForChild("Humanoid")
        local PlayerGui = Plr:WaitForChild("PlayerGui")
        local Vectors = PlayerGui:WaitForChild("Vectors")

        local P = Instance.new("Part")
        P.Name = Char.Name
        P.BrickColor = Vectors.Color.Value
        
        if Vectors.Anchor.Value == 1 then
            P.Anchored = true
        elseif Vectors.Anchor.Value == 0 then
            P.Anchored = false
        end

        P.Size = Vectors.VValue.Value
        P.Position = Humanoid.TargetPoint
        wait(2)
        P:Destroy()
    end

end

Tool.Activated:Connect(Place)
1 Like

Thanks for going out of your way and helping me out! But sadly the script doesn’t work… I don’t think its compatible with one of my GUI activations script.

Hm, would it be possible to check out your Gui Scripts that changes the Values of your Value Objects? And might I ask what type it is? (Script/LocalScript/ModuleScript)

Sure, the GUI script is ths:

function onActivate()
c = script.Parent.Vectors :Clone()
c.Parent = game.Players.LocalPlayer.PlayerGui
wait(.01)
script.Disabled = true
script.Parent.MasterScript.Disabled = false
c.AnchorL.LocalScript.Disabled = false
c.ColorL.ColorButton.Script.Disabled = false
c.ColorL.ColorButton.Frame.LocalScript.Disabled = false
c.TotalSize.LocalScript.Disabled = false
end

script.Parent.Activated:connect(onActivate)

And its a LocalScript.

Ah, I appreciate it

So there is actually a difference with changing Value Objects in the client & the server

They work differently, but if you attempt to value a Value Object (NumberValue, IntValue, StringValue, etc) from the client, it will not replicate towards the server because only the client can see what’s changed from this side (I believe this is set up due to hackers from preventing to exploit replication)

What we can probably use, is something that’s called a RemoteEvent & this object can be very handy for handling one-way transportations from the client-server/server-client

What we’ll use, are 2 functions/events that this RemoteEvent has:

  • FireServer() would fire a event from the client, to the server (This can only be used in LocalScripts)
  • OnServerEvent would detect & recieve those said “events” from the client side (This can only be used in Scripts)

But from what you sent me, I don’t really see any .Value changes you’re making :thinking: Could you send a LocalScript that changes the Values inside your PlayerGui?

1 Like

I understand what you are asking… I think :joy: but I don’t have any other scripts that could possibly have that.

Since you disable the script the script stops, change it to this

script.Parent.Activated:Connect(function()
	local c = script.Parent.Vectors:Clone()
	c.Parent = game:GetService("Players").LocalPlayer.PlayerGui
	task.wait(.01)
	script.Parent.MasterScript.Disabled = false
	c.AnchorL.LocalScript.Disabled = false
	c.ColorL.ColorButton.Script.Disabled = false
	c.ColorL.ColorButton.Frame.LocalScript.Disabled = false
	c.TotalSize.LocalScript.Disabled = false
	script.Disabled = true
end)

have find that everything that is inside will be disconnected.


With the first script, Debris schedules the destruction of an Instance.

local Player = game:GetService("Players").LocalPlayer

script.Parent.Activated:Connect(function()
	local p = Instance.new("Part")
	p.Parent = workspace
	p.Name = Player.Name
	p.BrickColor = Player.PlayerGui.Vectors.Color.Value
	if Player.PlayerGui.Vectors.Anchor.Value == 1 then
		p.Anchored = true
	elseif Player.PlayerGui.Vectors.Anchor.Value == 0 then
		p.Anchored = false
	end
	p.Size = Player.PlayerGui.Vectors.VValue.Value
	p.Position = Player.Character.Humanoid.TargetPoint
	game:GetService("Debris"):AddItem(p, 2)
end)
2 Likes

Wow! Thanks so much! its works! :slight_smile:

1 Like