Hello! Right now, I’m trying to make a box tool, kind of similar to the ladder of Steep Steps, where you place it wherever your mouse clicks. My problem is that it just doesn’t work.
Server script:
--Services
local Players = game:GetService("Players")
--Ladder variables
local toolEquipped = false
local tool = script.Parent
local Box = tool.BoxServer:WaitForChild("Box")
local BoxValue = tool.BoxValue
local BoxEvent = tool:WaitForChild("BoxEvent")
local currentBox
--Player variables
local Player = tool.Parent.Parent
local Character = Player.CharacterAdded:Wait()
local function boxPosition(rootPart)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {Character:GetDescendants()}
params.FilterType = Enum.RaycastFilterType.Exclude
local position
local ray = game.Workspace:Raycast(rootPart.Position, rootPart.CFrame.LookVector * 5)
if ray then
if ray.Instance then
local distance = (rootPart.Position - ray.Position).Magnitude
position = rootPart.CFrame * CFrame.new(0, 2.5, - distance)
end
else
position = rootPart.CFrame * CFrame.new(0, 2.5, -5)
end
return position
end
Character.Humanoid.Died:Connect(function()
if currentBox then
currentBox:Destroy()
BoxValue = nil
end
tool:Destroy()
end)
tool.Activated:Connect(function()
if not BoxValue.Value then
currentBox = Box:Clone()
BoxValue.Value = currentBox
currentBox.Parent = game.Workspace
currentBox.CanCollide = true
currentBox.Transparency = 0
currentBox.Anchored = false
currentBox.CFrame = boxPosition(Character.HumanoidRootPart)
currentBox.AncestryChanged:Connect(function()
BoxValue.Value = nil
end)
end
end)
BoxEvent.OnServerEvent:Connect(function(owner)
if BoxValue.Value then
if Player == owner then
currentBox:Destroy()
end
end
end)
Client script:
--Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
--Player variables
local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
--Ladder variables
local toolEquipped = false
local tool = script.Parent
local Box = tool.BoxServer:WaitForChild("Box")
local BoxPreview = script.Preview:Clone()
local BoxValue = tool.BoxValue
local BoxEvent = tool:WaitForChild("BoxEvent")
local currentBox
--Mouse variables
local Mouse = Player:GetMouse()
local removeDistance = 10
tool.Equipped:Connect(function()
toolEquipped = true
if BoxValue.Value == nil then
BoxPreview.Transparency = 0.5
BoxPreview.Parent = game.Workspace
end
Character.Humanoid.Died:Connect(function() -- Remove preview upon death
BoxPreview:Destroy()
end)
end)
tool.Unequipped:Connect(function()
toolEquipped = false
currentBox.SelectionBox.Visible = false
BoxPreview.Transparency = 1
BoxPreview.Parent = nil
end)
tool.Activated:Connect(function()
BoxPreview.Transparency = 1
BoxPreview.Parent = nil
end)
RunService.Heartbeat:Connect(function()
if toolEquipped then
if BoxValue.Value == nil then
local params = RaycastParams.new()
params.FilterDescendantsInstances = {Character:GetDescendants()}
params.FilterType = Enum.RaycastFilterType.Exclude
local ray = game.Workspace:Raycast(HumanoidRootPart.Position, HumanoidRootPart.CFrame.LookVector * 5, params)
if ray then
if ray.Instance then
local distance = (HumanoidRootPart.Position - ray.Position).Magnitude
BoxPreview.CFrame = HumanoidRootPart.CFrame * CFrame.new(0, 2.5, - distance)
end
else
BoxPreview.CFrame = HumanoidRootPart.CFrame * CFrame.new(0, 2.5, -5)
end
elseif BoxValue.Value then
currentBox = BoxValue.Value
if Mouse.Target == currentBox then
local distance = (currentBox.Position - HumanoidRootPart.Position).Magnitude
if distance <= removeDistance then
currentBox.SelectionBox.Visible = true
elseif distance > removeDistance then
currentBox.SelectionBox.Visible = false
end
else
currentBox.SelectionBox.Visible = false
end
end
end
end)
UserInputService.InputBegan:Connect(function(input, processed)
currentBox = BoxValue.Value
if not processed then
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
if toolEquipped and currentBox then
if Mouse.Target == currentBox then
local distance = (currentBox.Position - HumanoidRootPart.Position).Magnitude
if distance <= removeDistance then
BoxEvent:FireServer()
BoxPreview.Transparency = .5
BoxPreview.Parent = game.Workspace
end
end
end
end
end
end)
Hierarchy:
If you think you know what went wrong, please let me know. Have a wonderful day!