Hello! So i have been making a system where you can break objects.
These are the 2 script I made:
local objectClicked = game.ReplicatedStorage:WaitForChild("ObjectClicked")
local Damage = 20
objectClicked.OnServerEvent:Connect(function(player,model)
if not model then return end
local healthAmount = model:GetAttribute("Health")
if healthAmount then
if healthAmount - Damage <= 0 then
model:Destroy()
else
model:SetAttribute("Health",healthAmount - Damage)
model.Health.Background.Foreground.Size = UDim2.fromScale(healthAmount / 100,1)
end
end
end)
local UserInputService = game:GetService("UserInputService")
local objectClicked = game.ReplicatedStorage:WaitForChild("ObjectClicked")
UserInputService.InputBegan:Connect(function(input,GPE)
if GPE then return end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 and input.UserInputType ~= Enum.UserInputType.Touch then return end
local RaycastParamsVariable = RaycastParams.new()
RaycastParamsVariable.FilterType = Enum.RaycastFilterType.Whitelist
RaycastParamsVariable.IgnoreWater = true
RaycastParamsVariable.FilterDescendantsInstances = game.Workspace.BreakableObjects:GetChildren()
local MousePosition = UserInputService:GetMouseLocation()
local MouseRay = game.Workspace.Camera:ScreenPointToRay(MousePosition.X,MousePosition.Y)
local Direction = MouseRay.Direction * 1000
local Result = game.Workspace:Raycast(MouseRay.Origin, Direction, RaycastParamsVariable)
if not Result then return end
if Result.Instance then
local Model = Result.Instance:FindFirstAncestorOfClass("Model")
if not Model then warn("There is no model for this object") return end
print(Model.Name)
objectClicked:FireServer(Model)
end
end)
What I would like to achieve is to make it so when you destroy the objects they would go into your inventory. I am relatively new to scripting and dont know how I would do this. All help is appreciated <3