I’m trying to make a bomb for my game, and today I’ve decided I wanted to make a way that the bombs can plant themselves. I would’ve done this by raycasting by the player’s height and then I made the bomb go to the designated position, though it’s not working and instead returns as nil when I print the instance.
The code goes as follows:
UserInputService.InputBegan:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.R then
print("e")
local bombPlantPosition
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {Tool.Parent}
local raycastResult = workspace:Raycast((Tool.Handle.CFrame * CFrame.new(0, 0, 2)).Position, Vector3.new(0, -5, 0), rayParams)
local castingRay = true
print("b")
while castingRay do task.wait()
print("a")
raycastResult = workspace:Raycast((Tool.Handle.CFrame * CFrame.new(0, 0, 2)).Position, Vector3.new(0, -5, 0), rayParams)
print(raycastResult)
if raycastResult ~= nil then
print("o")
bombPlantPosition = raycastResult.Position
print(bombPlantPosition)
castingRay = false
if not cooldown then
cooldown = true
RemoteEvent:FireServer(Player, bombPlantPosition)
UserInputService.MouseIcon = RELOADING_ICON
task.wait(5)
UserInputService.MouseIcon = MOUSE_ICON
cooldown = false
end
end
end
end
end)