Hello there this is my first time using this category
Anyhow like the title says I want to make a breakable block that only breaks when a key is pressed and the characters Are on top of it. Please see the video below on what I want to achieve
Thank you
Note: I’m not asking you to spoon feed me code any tut or explanation would be nice
This will tell you what object the player is touching.
local Player = game:GetService("Players").LocalPlayer
Character = Player.Character or Player.CharacterAdded:Wait()
local Bool = false
for i,Part in pairs(Character:GetDescendants()) do
Part.Touched:Connect(function(hit)
if not hit:IsDescendantOf(Character) then
print(hit)
if Part.Name == "Destroy" then
Bool = true
elseif Part.Name == "Floor" then
Bool = false
end
end
end)
end
local blockWasTouched = false -- bool value
BasePart.Touched:Connect(function(part)
if not blockWasTouched then
blockWasTouched = true
-- the script that runs the runs what's suppose to happen
end
end)
Unless you mean making the script detect when a bool value in created in the explorer
Instance.ChildAdded:Connect(function(child)
if child:IsA("BoolValue") then
-- do stuff
end
end)
You could use events for that
Event:Wait() -- this stops the script until the event is fired
Yes, if you want to check if whatever was added was a BoolValue
local function WaitForChild(parent, child) : Instance -- function to wait for a child to be added
local c = parent.ChildAdded:Wait() -- wait for the child to be added
if c:IsA(child) then -- check if it's the specified type
return c -- return it
else
return WaitForChild(parent, child) -- repeat
end
end
local boolValue = WaitForChild(Instance, "BoolValue")
Depending on how you want to handle it, you could have a true/false variable that toggles when the blocks are near you. Then if it’s on (when the blocks are detected), you can allow the player to press a key to break them.
Or blocks can constantly be checked around you, and if they detect the breakable blocks (via certain names, part has something inside of it, etc.), you could be able to break them.
In the case you want to do the latter, RunService could be a benefit for that.
I would put an object you would barely or never use inside of the breakable object so the script can detect it from FindFirstChild, for example I would put a beam inside of it so a script can find every block that has the beam inside and destroy it if its in the region3. Probably not efficient but it works for me.