Help with script add block when someone clicks on a Gui button

spawner place:
image
spawner part:
image
part template:
image
gui:
image
in localscript:

local partsSpawned = game.Workspace.partsSpawned
local repstorage = game:GetService("ReplicatedStorage")
local spawnpart = repstorage.part
local spawner = game.Workspace:WaitForChild("spawner")
local button = script.Parent

button.MouseButton1Down:Connect(function()
	local new = spawnpart:Clone()
	new.Parent = partsSpawned
	new.CFrame = spawner.CFrame
end)

Problem is:

When anyone clicks on the button:


It spawns part but if click on the button again:

It does not want to put another spawned part in the old part
How can I make it if anyone clicks on the button and click the button again spawns another part in the old part
Can someone help me?

3 Likes

i’m pretty sure it spawned the part again but just in the same place so it appears as one

3 Likes

How can I change the new part location to the same spawner part location but makes the new part in the bottom of the old part

2 Likes

`
local partsSpawned = game.Workspace.partsSpawned
local repstorage = game:GetService(“ReplicatedStorage”)
local spawnpart = repstorage.part
local spawner = game.Workspace:WaitForChild(“spawner”)
local button = script.Parent
local Cf
button.MouseButton1Down:Connect(function()
local new = spawnpart:Clone()
new.Parent = partsSpawned
if Cf then
Cf = Cf * CFrame.new(0,1,0) ----- u can change that hovewer you want
else
Cf = spawner.CFrame
end
new.CFrame = Cf

end)
`
thatll propably work.

2 Likes

Try to do this

new.CFrame = spawner.CFrame * CFrame.new(0,0.5,0):Inverse()
2 Likes

That will still put all the parts in the same position.

2 Likes

Since you want the new parts to get below the old ones its a bit tricky but a simple loop to move all the old ones up should do it.

local partsSpawned = workspace:WaitForChild("partsSpawned")
local repstorage = game:GetService("ReplicatedStorage")
local spawnpart = repstorage.part
local spawner = game.Workspace:WaitForChild("spawner")
local button = script.Parent
local partY = spawnpart.Size.Y


local function MovePartsUp()
	for i,v in partsSpawned:GetChildren() do
		v.CFrame *= CFrame.new(0,partY,0)
	end
end



button.MouseButton1Down:Connect(function()
	MovePartsUp()
	local new = spawnpart:Clone()
	new.Parent = partsSpawned
	new.CFrame = spawner.CFrame
	
end)
2 Likes

It’s working but I mean:
image
The top of the part:
image
Sorry

2 Likes

you mean you want the new parts to be on top of the old ones ?

2 Likes

I mean it, But I said the top of the part mistake
I mean the top of the part

2 Likes

then use a counter and multiply it by the Y size of the part everytime :

local partsSpawned = workspace:WaitForChild("partsSpawned")
local repstorage = game:GetService("ReplicatedStorage")
local spawnpart = repstorage.part
local spawner = game.Workspace:WaitForChild("spawner")
local button = script.Parent
local partY = spawnpart.Size.Y

local counter = 0


button.MouseButton1Down:Connect(function()
	
	local new = spawnpart:Clone()
	new.Parent = partsSpawned
	new.CFrame = spawner.CFrame * CFrame.new(0,partY * counter,0)
    counter += 1
	
end)
3 Likes

Thanks for your responds guys!
Thanks for everyone replied to this topic
@Fimutsuu Thanks. It works

3 Likes

One question more:
Can I put this in a server script (in ServerScriptService):

game.Players.PlayerAdded:Connect(function(plr)
	while wait(1) do
		if plr.Character then
			local partsSpawned = workspace:WaitForChild("partsSpawned")
			local repstorage = game:GetService("ReplicatedStorage")
			local spawnpart = repstorage.part
			local spawner = game.Workspace:WaitForChild("spawner")
			local button = script.Parent
			local partY = spawnpart.Size.Y

			local counter = 0



			local new = spawnpart:Clone()
			new.Parent = partsSpawned
			new.CFrame = spawner.CFrame * CFrame.new(0,partY * counter,0)
			counter += 1
		end
	end
end)

Or I need to edit somethings?

2 Likes

you can but i don’t understand the point of it. You’re trying to add the part from the server so every player will see it, but at the same time you’re doing it for all the players.
Also do you want a part to be added every second?

2 Likes

Yeah, I want to make a game like +1 block every second or get richer every second
and I needed a script that spawn part every second or when anyone click on the button

1 Like

Here a problem:

game.Players.PlayerAdded:Connect(function(plr)
	while wait(1) do
		if plr.Character then
			local partsSpawned = workspace:WaitForChild("partsSpawned")
			local repstorage = game:GetService("ReplicatedStorage")
			local spawnpart = repstorage.part
			local spawner = game.Workspace:WaitForChild("spawner")
			local button = script.Parent
			local partY = spawnpart.Size.Y

			local counter = 0

			local new = spawnpart:Clone()
			new.Parent = partsSpawned
			new.CFrame = spawner.CFrame * CFrame.new(0,partY * counter,0)
			counter += 1
		end
	end
end)

image
But it glitched:
image

1 Like

First of all if you put this in ServerScriptService, there is no such thing button = script.Parent remove that, also … you know what i’ll just fix the script here:

local partsSpawned = workspace:WaitForChild("partsSpawned")
local repstorage = game:GetService("ReplicatedStorage")
local spawnpart = repstorage.part
local spawner = game.Workspace:WaitForChild("spawner")
local partY = spawnpart.Size.Y
game.Players.PlayerAdded:Connect(function(plr)
	local counter = 0
    while task.wait(1) do
		if plr.Character then
			local new = spawnpart:Clone()
			new.Parent = partsSpawned
			new.CFrame = spawner.CFrame * CFrame.new(0,partY * counter,0)
			counter += 1
		end
	end
end)
2 Likes

image
It works thanks

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.