I have a small issue. Two of them, apparently.

local InputService = game:GetService"UserInputService"
local Tool = script.Parent

local function getPreview(model)
	local Preview = model:Clone()
	
	local function clean(model)
		for _, v in ipairs(model:GetChildren()) do
			if v:IsA"BasePart" then
				v.CanCollide = false
				v.Transparency = math.max(v.Transparency, 0.5)
				clean(v)
			elseif v:IsA"Decal" then
				v.Transparency = math.max(v.Transparency, 0.5)
			elseif v:IsA"Model" then
				clean(v)
			else
				v:Destroy()
			end
		end
	end
	clean(Preview)
	
	return Preview
end

local function getModelCenter(model)
	local center
	
	for _, v in ipairs(model:GetChildren()) do
		if v:IsA"BasePart" then
			if center == nil then
				center = v.Position
			else
				center = center:lerp(v.Position, .5)
			end
		elseif center == nil then
			center = getModelCenter(v)
		else
			center = center:lerp(getModelCenter(v) or center, .5)
		end
	end
	
	return center
end

local PivotPart = Instance.new"Part"
PivotPart.Name = "PrimaryPart"
PivotPart.Transparency = 1
PivotPart.Size = Vector3.new(1, 1, 1)
PivotPart.Anchored = true
PivotPart.CanCollide = false
PivotPart.Archivable = false
PivotPart.TopSurface = "Smooth"
PivotPart.BottomSurface = "Smooth"
local function pivotModel(model, amount)
	if model.PrimaryPart ~= nil then
		model:SetPrimaryPartCFrame(model:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(amount), 0))
	else
		PivotPart.CFrame = CFrame.new(getModelCenter(model))
		PivotPart.Parent = model
		model.PrimaryPart = PivotPart
		model:SetPrimaryPartCFrame(model:GetPrimaryPartCFrame() * CFrame.Angles(0, math.rad(amount), 0))
		model.PrimaryPart = nil
		PivotPart.Parent = nil
	end
end

local connections = {}

Tool.Equipped:connect(function(mouse)
	if Tool.Model.Value == nil then
		warn"The Placer tool is missing an Object. Please set the ObjectValue's Value."
		return
	elseif #Tool.Model.Value:GetChildren() < 1 then
		warn"The Placer tool requires a model with at least one physical object. Please fill the model linked to the Placer tool before using it."
		return
	end
	
	local Model = Tool.Model.Value:Clone()
	local Preview = getPreview(Model)
	mouse.TargetFilter = Preview
	Preview:MoveTo(mouse.Hit.p)
	Preview.Archivable = false
	Preview.Parent = workspace
	
	connections[1] = mouse.Move:connect(function()
		Preview:MoveTo(mouse.Hit.p)
	end)
	
	connections[2] = InputService.InputBegan:connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			if InputService:IsKeyDown(Enum.KeyCode.LeftShift) or InputService:IsKeyDown(Enum.KeyCode.RightShift) then
				pivotModel(Model, -45)
				pivotModel(Preview, -45)
			else
				pivotModel(Model, 45)
				pivotModel(Preview, 45)
			end
		end
	end)
	
	connections[3] = Tool.Unequipped:connect(function()
		Preview:Destroy()		
		Model:Destroy()
		
		for _, conn in ipairs(connections) do
			conn:disconnect()
		end
	end)
	
	mouse.Button1Down:wait()
	
	for _, conn in ipairs(connections) do
		conn:disconnect()
	end

	Model.Parent = workspace
	Model:MoveTo(mouse.Hit.p)
end)

There is a Tool in which there is ObjectValue called Model (in ROBLOX Studio you select the model which spawns itself in Workspace) and the Script above.

The issue is, that two Grenades spawn right away in the Workspace, not one.

The other issue is that I wish the player could put the Grenade multiple times, since after these two Grenades at once are put, the player can't put any more.

Thank you.

2 comments

0

Can you specify the possible code lines of issues?

0

I already found a solution myself. Thank you anyways. I appreciate it that you tried to help. :)