Help: Spawn part front of player when click on gui button

How can I make it when a player clicks a button in Gui to spawn a part in front of the player?

2 Likes

Does anyone know how to make it?

This is very simple and dose not need a post to solve. u can always look up how to spawn a part in front of a player. and use that method in a button event. But here is how to do it


local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")

local button: GuiButton = ...

function SpawnPart(IsAnchored: boolean)
    local Part = Instance.new("Part")
    Part.Anchored = IsAnchored
    Part.CFrame = HRP.CFrame * CFrame.new(0,0, -3)
    Part.Parent = workspace
end

button.MouseButton1Down:Connect(function(x, y)
    SpawnPart()
end)

very simple.
here is the post u should of went to

You Could Try This:

local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(Player)

local ScreenGui = “Get ScreenGui”

local UI = “Get UI”

UI.MouseButton1Click:Connect(function()

local Part = “Get Part”

Part.CFrame = Player.Character:WaitForChild(“HumanoidRootPart”).CFrame * CFrame.new(0, 0, -2.5)
print(“Worked?”)

end)

end)

I Wrote This On A Mobile Device So Forgive Me For Any Typos, But This Should Work. (Replace The Variables With Quotes With The Actual Instance Location.)

I woke up, I gonna open Roblox studio and trying the scripts

This script is easy and works thx

Not working, Sry man

Don’t Worry, I Found A Solution!

Place A LocalScript In StartPlayerScripts.

Screenshot (51)

After That, Copy The Following Code (Assuming You Understand It.):

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local Character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()

local ScreenGui = Instance.new("ScreenGui", Players.LocalPlayer.PlayerGui)

local Button = Instance.new("TextButton")
Button.Text = ""
Button.Size = UDim2.fromOffset(100, 100)
Button.Position = UDim2.fromScale(.8, .5)
Button.AnchorPoint = Vector2.new(.5, .5)
Button.Parent = ScreenGui

Button.MouseButton1Click:Connect(function()

	local Part = Instance.new("Part")
	Part.Size = Vector3.new(3, 3, 3)
	Part.CanCollide = false
	Part.Anchored = true
	Part.Parent = workspace

	local PreRender = RunService.PreRender:Connect(function()

		Part.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3.5)

	end)
	
	UserInputService.InputBegan:Connect(function(input)
		
		if input.KeyCode == Enum.KeyCode.F then
			
			PreRender:Disconnect()
			Part:Destroy()
			print("Part Removed.")
			
		end
		
	end)

end)

You can change the “Enum.KeyCode.F” to whatever key you want.

K I gonna try it

1 Like

Nice works thx too, I gonna use it and use the other one

Glad i could, i wish you the best of luck on your game

I edited the script to be when someone click on the button spawn a part front of the player and the player have tool to burn the part to get cash:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local Character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()

local Button = script.Parent
counter = 0
canspawnpart = true

Button.MouseButton1Click:Connect(function()
	if counter >= 0 then
		if counter == 5 then
			canspawnpart = false
		end
		if canspawnpart == true then
			local Part = Instance.new("Part")
			Part.Size = Vector3.new(3, 3, 3)
			Part.CanCollide = true
			Part.Anchored = false
			Part.Parent = workspace
			counter += 1

			Part.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -4)

			UserInputService.InputBegan:Connect(function(input)

				if input.KeyCode == Enum.KeyCode.F then

					Part:Destroy()
					print("Part Removed.")
					counter = 0
					canspawnpart = true
				end
			end)
		end
	end
end)

Nice, may i ask what game your making?

burn to get MONEY
Stupid game ig

That Sounds Fun, I Would Play as i like clicker games and other games of that nature.


Here a video hehe>>>>>>>>>>
Here music:
image
Spotify

The Concept Is A Neat Idea Already, Can’t Wait To See The Full Game.

image
How can I make this image to picture of the part without screenshot the part with local script?

Place The Following Instances In Order Into StarterGui:

Screenshot (54)

Set The ViewportFrame’s CurrentCamera To The Camera In The ViewportFrame Like So:

Screenshot (57)

Set The Camera CFrame Like So:

Screenshot (56)

Set The Part Properties Like So:

Screenshot (55)

That’s All

not working:
image